1

所以我编写了一个 JSON 文件代码来帮助显示书籍的标题,但是当我编译时,我一直收到同样的错误,有没有办法修复或者我做错了什么?

代码如下:

jsoniq version "1.0";
let $file: = {
    {
        "title": "Fifty Shades of Grey",
        "author": "E.L.",
        "date_read": {
            "month": "May",
            "year": "2016"
        },
        "opinion": "Did not like very much"
    },
    {
        "title": "The grass is singing",
        "author": "Doris Lessing",
        "date_read": {
            "month": "June",
            "year": "2016"
        },
        "opinion": "Enjoyed quite a bit"
    },
    {
        "title": "A short history on nearly everything",
        "author": "Bill Bryson",
        "date_read": {
            "month": "July",
            "year": "2016"
        },
        "opinion": "Very informative"
    },
    {
        "title": "JSON in 24 hours",
        "author": "Peter Settler",
        "purpose": "Work"
    },
    {}
}
for $x in $file
return $x.title

已显示的错误:

Error: Parse error on line 1:
jsoniq version "1.0"
^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
4

1 回答 1

1

JSONiq 代码有两个问题:

  • 之间:=之后的空间$file
  • (...)应该用于创建对象序列,而不是{...}用于{...}创建 JSON 对象(作为键值对列表)。

我希望这有帮助!这个查询应该可以工作(我用RumbleDB测试过):

jsoniq version "1.0";
let $file := (
  { "title": "Fifty Shades of Grey", "author": "E.L.", "date_read": { "month": "May", "year": "2016" }, "opinion": "Did not like very much" },
  { "title": "The grass is singing", "author": "Doris Lessing", "date_read": { "month": "June", "year": "2016" }, "opinion": "Enjoyed quite a bit" },
  { "title": "A short history on nearly everything", "author": "Bill Bryson", "date_read": { "month": "July", "year": "2016" }, "opinion": "Very informative" },
  { "title": "JSON in 24 hours", "author": "Peter Settler", "purpose": "Work" },
  {}
)
for $x in $file
return $x.title

另一个想法:您收到的错误消息可能表明您正在尝试将此代码输入 JSON 解析器而不是 JSONiq 引擎。从语法上讲,JSON 是 JSONiq 的一个子集,因此您可以将 JSON 输入任何 JSONiq 引擎,它会“返回自身”。但是,JSONiq 不是 JSON 的子集,因此它不能反过来工作。

于 2021-08-12T12:29:26.467 回答