这是有效的 JSON 吗? 错误:prolog 中不允许的内容



如何测试以下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$ 

我通过格式化程序运行原始数据以使其更具可读性:

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:nnh......... 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

引用文档进行CREATE DATABASE

语法CREATE DB [name] ([input])

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

如您所见,该命令需要一个 XML 文件,而不是 JSON 文件。

似乎有效:

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).
> 
> 

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

只是想用Java做上述事情。 关于JSON与XML的观点是正确的。 (xquery文件来自文档。

如果目录中只有格式化的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 /

然后返回预期结果(完整JSON(。

最新更新