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

时间:2022-08-23 07:30:51

I tried to parse this string :

我试图解析这个字符串:

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

Using JSON.parse() on this string show me "undefined" in the console. According to this site, my json is valid. It comes from a json_encode given by a php function.

在此字符串上使用JSON.parse()会在控制台中显示“undefined”。根据这个网站,我的json是有效的。它来自php函数给出的json_encode。

If it can help, the final goal is to loop through this json array. Thanks.

如果它可以提供帮助,那么最终的目标是遍历这个json数组。谢谢。

[EDIT]

I realized that my error was in fact a scope issue using literal functions. Yes, I'm a bit stupid sometimes. Thanks everybody for your help!

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

2 个解决方案

#1


5  

This is no String, its a valid JSON which you can use in JavaScript:

这不是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


0  

If your having a return like this

如果你有这样的回报

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

then you can use the JSON.parse() function

那么你可以使用JSON.parse()函数

and it will decode the stringify json data

它将解码stringify json数据

and it will return you

它会回报你

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

since the return is already a json object then no need to use JSON.parse();

因为返回已经是一个json对象,所以不需要使用JSON.parse();

#1


5  

This is no String, its a valid JSON which you can use in JavaScript:

这不是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


0  

If your having a return like this

如果你有这样的回报

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

then you can use the JSON.parse() function

那么你可以使用JSON.parse()函数

and it will decode the stringify json data

它将解码stringify json数据

and it will return you

它会回报你

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

since the return is already a json object then no need to use JSON.parse();

因为返回已经是一个json对象,所以不需要使用JSON.parse();