用jquery解析JSON?

时间:2012-01-12 18:31:06

标签: javascript jquery json

当使用jquery解析从服务器返回的这个json数据时,我可以使用一些语法帮助。我已经尝试了stackoverflow和其他网站的几个例子,并且出于某种原因我不断定义为out out而不是id号。每一个都应该是它自己的一行。

{
  "ROWCOUNT":7,
  "COLUMNS":["ID"],
  "DATA":{"id":"211","212","213","221","222","223","232"]}
}

4 个答案:

答案 0 :(得分:5)

如果您通过JSONLint等验证程序传递JSON,您将看到JSON不正确,这可能是错误的原因。

这是JSONLint所显示的:

Parse error on line 8:
..."211",        "212",        "213",   
----------------------^
Expecting ':

添加一些空格时很容易发现:

{
    "ROWCOUNT": 7,
    "COLUMNS": [
        "ID"
    ],
    "DATA": {
        "id": "211",
        "212",
        "213",
        "221",
        "222",
        "223",
        "232"
    ]
}
}

答案 1 :(得分:0)

我不知道你到目前为止尝试解析它的代码,但我可以告诉你,上面的JSON代码在语法上是错误的。请改用:

{
  "ROWCOUNT":7,
  "COLUMNS":["ID"],
  "DATA":{"id":"211","212","213","221","222","223","232"}
}

你有一个额外的方括号。

答案 2 :(得分:0)

问题似乎是您在[声明后错过了一个开放的id。它应该如下所示

{"ROWCOUNT":7, "COLUMNS":["ID"], "DATA":{"id":["211","212","213","221","222","223","232"]}}

使用示例

var json = '{"ROWCOUNT":7, "COLUMNS":["ID"], "DATA":{"id":["211","212","213","221","222","223","232"]}}';
var obj = $.parseJSON(json);
console.log(obj);

答案 3 :(得分:0)

你的方括号语法错误:

  "DATA":{"id":"211","212","213","221","222","223","232"]}

应该是:

  "DATA":{"id":["211","212","213","221","222","223","232"]}