【问题标题】:JSON.parse is giving an "undefined" objectJSON.parse 给出了一个“未定义”的对象
【发布时间】:2015-04-28 09:02:00
【问题描述】:

我试图解析这个字符串:

[{"ZoneId": "1", "0": "1", "ZoneX": "29", "1": "29", "ZoneY": "27", "2":     "27", "ZoneWidth": "76", "3": "76", "ZoneHeight": "61", "4": "61", "ZoneImage": "46", "5": "46", "ZonePointTo": "2", "6": "2"},
{"ZoneId": "2", "0": "2", "ZoneX": "382", "1": "382", "ZoneY": "226", "2": "226", "ZoneWidth": "-117", "3": "-117", "ZoneHeight": "98", "4": "98", "ZoneImage": "46", "5": "46", "ZonePointTo": "3", "6": "3"},
{"ZoneId": "3", "0": "3", "ZoneX": "108", "1": "108", "ZoneY": "74", "2": "74", "ZoneWidth": "363", "3": "363", "ZoneHeight": "83", "4": "83", "ZoneImage": "46", "5": "46", "ZonePointTo": "2", "6": "2"}]

在此字符串上使用 JSON.parse() 在控制台中显示“未定义”。根据this site,我的 json 是有效的。它来自一个 php 函数给出的 json_encode。

如果有帮助,最终目标是循环遍历这个 json 数组。谢谢。

[编辑]

我意识到我的错误实际上是使用文字函数的范围问题。是的,有时候我有点傻。谢谢大家的帮助!

【问题讨论】:

  • 确保在将 " 输入到解析时正确转义。
  • 字符串在哪里?你是如何得到对象的?你是怎么解析的?你缺乏细节
  • JSFiddle 表明上述内容实际上可以与JSON.parse 一起正常工作。 jsfiddle.net/infiniteloops/sqwyxoo5
  • JSON.parse() 将“字符串”解析为 JSON。如果你需要用 JSON.parse 解析它,我认为一个简单的方法是用单引号括起来,使用双引号将不起作用,因为内容包含未转义的双引号。

标签: javascript arrays json parsing object-literal


【解决方案1】:

这不是String,它是一个可以在 JavaScript 中使用的有效 JSON:

var jsonData = [{"ZoneId": "1", "0": "1", "ZoneX": "29", "1": "29", "ZoneY": "27", "2":     "27", "ZoneWidth": "76", "3": "76", "ZoneHeight": "61", "4": "61", "ZoneImage": "46", "5": "46", "ZonePointTo": "2", "6": "2"},
{"ZoneId": "2", "0": "2", "ZoneX": "382", "1": "382", "ZoneY": "226", "2": "226", "ZoneWidth": "-117", "3": "-117", "ZoneHeight": "98", "4": "98", "ZoneImage": "46", "5": "46", "ZonePointTo": "3", "6": "3"},
{"ZoneId": "3", "0": "3", "ZoneX": "108", "1": "108", "ZoneY": "74", "2": "74", "ZoneWidth": "363", "3": "363", "ZoneHeight": "83", "4": "83", "ZoneImage": "46", "5": "46", "ZonePointTo": "2", "6": "2"}];

for(index in jsonData) {
    alert(JSON.stringify(jsonData[index]));
}

【讨论】:

    【解决方案2】:

    您的字符串不是 json 对象

    它是一个 json 数组对象(查看方括号)。
    因此,要获得值,您必须将其运行到“for”或“each”或 ...
    . 下面的每个示例
    将字符串传递给变量,然后;

    var obj=[{"ZoneId": "1", "0": "1", "ZoneX": "29", "1": "29", "ZoneY": "27", "2":     "27", "ZoneWidth": "76", "3": "76", "ZoneHeight": "61", "4": "61", "ZoneImage": "46", "5": "46", "ZonePointTo": "2", "6": "2"},
    {"ZoneId": "2", "0": "2", "ZoneX": "382", "1": "382", "ZoneY": "226", "2": "226", "ZoneWidth": "-117", "3": "-117", "ZoneHeight": "98", "4": "98", "ZoneImage": "46", "5": "46", "ZonePointTo": "3", "6": "3"},
    {"ZoneId": "3", "0": "3", "ZoneX": "108", "1": "108", "ZoneY": "74", "2": "74", "ZoneWidth": "363", "3": "363", "ZoneHeight": "83", "4": "83", "ZoneImage": "46", "5": "46", "ZonePointTo": "2", "6": "2"}]
    
                jQuery.each(obj, function(key,value) {
                    alert(value.ZoneId);
                });
    

    【讨论】:

      【解决方案3】:

      如果你有这样的回报

      var json_string =  "[{"0":"1","1":"29","2":"27","3":"76","4":"61","5":"46","6":"2","ZoneId":"1","ZoneX":"29","ZoneY":"27","ZoneWidth":"76","ZoneHeight":"61","ZoneImage":"46","ZonePointTo":"2"},{"0":"2","1":"382","2":"226","3":"-117","4":"98","5":"46","6":"3","ZoneId":"2","ZoneX":"382","ZoneY":"226","ZoneWidth":"-117","ZoneHeight":"98","ZoneImage":"46","ZonePointTo":"3"},{"0":"3","1":"108","2":"74","3":"363","4":"83","5":"46","6":"2","ZoneId":"3","ZoneX":"108","ZoneY":"74","ZoneWidth":"363","ZoneHeight":"83","ZoneImage":"46","ZonePointTo":"2"}]"
      

      然后你可以使用 JSON.parse() 函数

      它会解码stringify json数据

      它会回报你

       [{"ZoneId": "1", "0": "1", "ZoneX": "29", "1": "29", "ZoneY": "27", "2":     "27", "ZoneWidth": "76", "3": "76", "ZoneHeight": "61", "4": "61", "ZoneImage": "46", "5": "46", "ZonePointTo": "2", "6": "2"},
      {"ZoneId": "2", "0": "2", "ZoneX": "382", "1": "382", "ZoneY": "226", "2": "226", "ZoneWidth": "-117", "3": "-117", "ZoneHeight": "98", "4": "98", "ZoneImage": "46", "5": "46", "ZonePointTo": "3", "6": "3"},
      {"ZoneId": "3", "0": "3", "ZoneX": "108", "1": "108", "ZoneY": "74", "2": "74", "ZoneWidth": "363", "3": "363", "ZoneHeight": "83", "4": "83", "ZoneImage": "46", "5": "46", "ZonePointTo": "2", "6": "2"}]
      

      既然返回的已经是一个json对象那么就不需要使用JSON.parse();

      【讨论】:

        猜你喜欢
        • 2018-05-20
        • 1970-01-01
        • 1970-01-01
        • 2017-08-14
        • 1970-01-01
        • 2018-11-13
        • 1970-01-01
        • 2015-09-06
        • 1970-01-01
        相关资源
        最近更新 更多