这是有效的JSON吗?错误:序言中不允许包含内容

问题描述 投票:-1回答:2

如何测试以下JSON文件的正确性?

在命令行上使用basex

thufir@dur:~/json$ 
thufir@dur:~/json$ ls
formatted.json  raw.json
thufir@dur:~/json$ 
thufir@dur:~/json$ basex
BaseX 9.0.1 [Standalone]
Try 'help' to get more information.
> 
> CREATE DATABASE db raw.json
"/home/thufir/json/raw.json" (Line 1): Content is not allowed in prolog.
> 
> CREATE DATABASE db formatted.json
"/home/thufir/json/formatted.json" (Line 1): Content is not allowed in prolog.
> 
> exit
Have fun.
thufir@dur:~/json$ 

我通过formatter运行原始数据,以使其更具可读性:

thufir@dur:~/json$ 
thufir@dur:~/json$ cat formatted.json 
{
  "1224083010015956992": {
    "metadata": {
      "result_type": "recent",
      "iso_language_code": "en"
    },
    "in_reply_to_status_id_str": null,
    "in_reply_to_status_id": null,
    "created_at": "Sun Feb 02 21:31:46 +0000 2020",
    "in_reply_to_user_id_str": null,
    "source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Twitter Web App<\/a>",
    "retweeted_status": {
      "metadata": {
        "result_type": "recent",
        "iso_language_code": "en"
      },
      "in_reply_to_status_id_str": null,
      "in_reply_to_status_id": null,
      "created_at": "Sun Feb 02 20:53:32 +0000 2020",
      "in_reply_to_user_id_str": null,
      "source": "<a href=\"https://about.twitter.com/products/tweetdeck\" rel=\"nofollow\">TweetDeck<\/a>",
      "retweet_count": 3,
      "retweeted": false,
      "geo": null,
      "in_reply_to_screen_name": null,
      "is_quote_status": false,
      "id_str": "1224073388706189312",
      "in_reply_to_user_id": null,
      "favorite_count": 6,
      "id": 1224073388706189312,
      "text": "Myth of the 10x programmer:\n\nh......... particularly like the list of productivity improvement \"tools\" at the end.",
      "place": null,
      "lang": "en",
      "favorited": false,
      "possibly_sensitive": false,

鉴于联机分析器显示了数据并且可以浏览节点,所以看不到问题所在。

完整:

https://gist.github.com/THUFIR/ab9e1f77af92d4d984b268434afc01dd.js

java json xml parsing basex
2个回答
1
投票

CREATE DATABASE的报价文档:

语法 CREATE DATABASE

输入可以是XML文档的文件或目录路径,远程URL或包含XML的字符串

您可以看到,该命令需要一个XML文件,而不是JSON文件。


0
投票

似乎可以使用:

CREATE DB [name] ([input])

并且该查询将返回一个非常大的文档,太大而无法在此处粘贴。

只是想使用Java来完成上述操作。有关JSON与XML的观点是正确的。 (thufir@dur:~/json$ thufir@dur:~/json$ thufir@dur:~/json$ basex BaseX 9.0.1 [Standalone] Try 'help' to get more information. > > list Name Resources Size Input Path ----------------------------------------------------------------------------- com.w3schools.books 1 6290 https://www.w3schools.com/xml/books.xml w3school_data 1 5209 https://www.w3schools.com/xml/note.xml 2 database(s). > > exit See you. thufir@dur:~/json$ thufir@dur:~/json$ ls createDB.xquery formatted.json raw.json thufir@dur:~/json$ thufir@dur:~/json$ cat createDB.xquery let $database := "db" for $name in file:list('.', false(), '*.json') let $file := file:read-text($name) let $json := json:parse($file) return db:add($database, $json, $name) thufir@dur:~/json$ thufir@dur:~/json$ basex createDB.xquery Stopped at /home/thufir/json/createDB.xquery, 5/14: [db:open] Database 'db' was not found. thufir@dur:~/json$ thufir@dur:~/json$ basex BaseX 9.0.1 [Standalone] Try 'help' to get more information. > > create database db Database 'db' created in 269.32 ms. > > list Name Resources Size Input Path ----------------------------------------------------------------------------- com.w3schools.books 1 6290 https://www.w3schools.com/xml/books.xml db 0 4570 w3school_data 1 5209 https://www.w3schools.com/xml/note.xml 3 database(s). > > exit See you. thufir@dur:~/json$ thufir@dur:~/json$ basex createDB.xquery thufir@dur:~/json$ thufir@dur:~/json$ basex BaseX 9.0.1 [Standalone] Try 'help' to get more information. > > list Name Resources Size Input Path ------------------------------------------------------------------------------- com.w3schools.books 1 6290 https://www.w3schools.com/xml/books.xml db 2 196469 w3school_data 1 5209 https://www.w3schools.com/xml/note.xml 3 database(s). > > 文件为文档xquery。)

仅在目录中提供格式化的from,似乎效果更好:

JSON

然后返回预期结果(完整的thufir@dur:~/json$ thufir@dur:~/json$ basex BaseX 9.0.1 [Standalone] Try 'help' to get more information. > > list Name Resources Size Input Path ------------------------------------------------------------------------------- com.w3schools.books 1 6290 https://www.w3schools.com/xml/books.xml db 1 101838 w3school_data 1 5209 https://www.w3schools.com/xml/note.xml 3 database(s). > > open db Database 'db' was opened in 66.85 ms. > > xquery /

© www.soinside.com 2019 - 2024. All rights reserved.