在JSON中表示矩阵

9

我正在尝试将矩阵样式的数据转换为JSON格式,但似乎无法实现。有人能帮我理解我做错了什么吗?

        {
   "took": 12,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 44,
      "max_score": 1,
      "hits": [
      {
            "_index": "transactions",
            "_type": "transaction",
            "_id": "trans0007",
            "_score": 1,
            "_source": {
                "fundRelation": "[1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0],
                                [0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0],
                                [0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [1,0,0,1,0,0,1,0,0,1,0,1,0,1,1,1,0,1,0,0],
                                [0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0],
                                [0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0],
                                [0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0],
                                [0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0],
                                [1,0,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,1,1,0],
                                [1,0,0,1,0,0,1,0,1,1,0,1,0,1,0,1,0,1,1,0],
                                [1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,0,0,1,0,0],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0],
                                [0,0,1,1,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0],
                                [0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0],
                                [0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0]
                            ",
                "fundName": ["Fund A","Fund B","Fund C","Fund D","Fund E","Fund F","Fund G","Fund H","Fund I","Fund J","Fund K","Fund L","Fund M","Fund N","Fund O","Fund P","Fund Q","Fund R","Fund S","Fund T"],
                "fundColor": ["#9ACD32","#377DB8","#F5DEB3","#EE82EE","#40E0D0","#FF6347","#D8BFD8","#D2B48C","#4682B4","#00FF7F","#FFFAFA","#708090","#708090","#6A5ACD","#87CEEB","#A0522D","#FFF5EE","#2E8B57","#F4A460","#FA8072"]
            }
         }     ]
   }
}

我不确定自己做错了什么。

我收到了以下错误信息:

> Parse error on line 19: ...    "fundRelation": "[1,0,0,1,0,0,1,0,1,
> -----------------------^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

http://jsonlint.com/

3个回答

7

问题在于,您正在尝试将多行字符串值分配给fundRelation,这不是有效的JSON格式。

....
"fundRelation": "[1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0],[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]",
...

或者你可以像这样做:

{
    "took": 12,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 44,
        "max_score": 1,
        "hits": [
            {
                "_index": "transactions",
                "_type": "transaction",
                "_id": "trans0007",
                "_score": 1,
                "_source": {
                    "fundRelation": [
                        [1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0],
                        [1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0]
                    ],
                    "fundName": ["Fund A","Fund B","Fund C","Fund D","Fund E","Fund F","Fund G","Fund H","Fund I","Fund J","Fund K","Fund L","Fund M","Fund N","Fund O","Fund P","Fund Q","Fund R","Fund S","Fund T"],
                    "fundColor":["#9ACD32","#377DB8","#F5DEB3","#EE82EE","#40E0D0","#FF6347","#D8BFD8","#D2B48C","#4682B4","#00FF7F","#FFFAFA","#708090","#708090","#6A5ACD","#87CEEB","#A0522D","#FFF5EE","#2E8B57","#F4A460","#FA8072"]
                }
            }
        ]
    }
}

6

在JavaScript中无法使用多行字符串(除非在新的引擎中使用`运算符)。您需要通过在每行结尾添加\来转义fundRelation的每一行结尾。

或者,不要将矩阵数据存储为字符串。删除数组开头和结尾的引号,并将其存储为标准数组。


点赞了Patrick的回答,因为我有点在重复他的话,并且他先发表了。:-) - Marin

4

看起来在多行拆分中使用 " 不太好。 无论如何,应该像这样:

...
"fundRelation": [
    [1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0],
    [0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    ...
    [0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0]
]
...

请注意额外的括号[...]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接