用JSON表示矩阵

时间:2015-08-22 05:13:59

标签: json matrix

我正在尝试在JSON中使用矩阵样式数据,但它似乎不起作用。谁能帮我理解我做错了什么?

qry = "select * from table where field1 = %s and field2=%s" %(value1, value2)
cr.execute(qry)

不确定我做错了什么。

我收到以下错误消息:

        {
   "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"]
            }
         }     ]
   }
}

http://jsonlint.com/

3 个答案:

答案 0 :(得分: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"]
                }
            }
        ]
    }
}

答案 1 :(得分:5)

你不能在javascript中使用多行字符串(除了在使用`运算符的新引擎中)。您需要通过在每行的末尾添加\来逃避每行fundRelation的结尾。

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

答案 2 :(得分:2)

看起来“拆分多行”并不合适。 无论如何,不​​应该是这样的:

...
"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]
]
...

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