当前位置: 首页 > 知识库问答 >
问题:

JSON无法编译,原因是:应为“字符串”、“数字”、“NULL”、“TRUE”、“FALSE”、“{”、“[”,得到了“未定义的”

蔡弘扬
2023-03-14

所以我写了一个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'

共有1个答案

葛成双
2023-03-14

JSO 尼克代码存在两个问题:

  • $file
  • (…)应该用于创建对象序列,而不是 {…} ,因为

我希望这有帮助!这个查询应该可以工作(我用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的子集,因此它不能以相反的方式工作。

 类似资料: