繁体   English   中英

JSON.parse 给出了一个“未定义”的对象

[英]JSON.parse is giving an “undefined” object

我试图解析这个字符串:

[{"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() 在控制台中显示“未定义”。 根据这个站点,我的 json 是有效的。 它来自 php 函数给出的 json_encode。

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

[编辑]

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

这不是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]));
}

你的字符串不是 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);
            });

如果你有这样的回报

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM