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数组。谢谢
[EDIT]
我意识到我的错误实际上是使用文字函数的范围问题。是的,我有时有点笨。感谢大家的帮助!
这不是String
,它是一个有效的JSON,可以在JavaScript:中使用
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数组对象(请检查方括号)
因此,要获得该值,您必须将其运行到";对于";或";每个";或者
.下面的每个示例
将字符串传递给变量,然后;
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()函数
它将解码字符串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();
相关文章:
- 变量转换为另一个“未定义”的变量
- getDataAsJSON()在PHP中是一个未定义的函数,但许多解释如何使用JSONP的网站都说要使用它
- 所以这是说我的法定对象中有一个未定义的变量
- 为什么在这个数组的末尾会有一个未定义的
- 为什么我得到一个未定义的回报
- Router.use()需要中间件函数,但得到了一个未定义的函数
- 使用jQuery'在数组中循环;s中的每一个都会在生成字符串时在开头产生一个未定义的值
- 如果定义了值,为什么会得到一个未定义的值?使用 eval()
- 无论如何,在Visual Studio智能感知中定义一个未定义的对象
- 角度过滤器分组依据过滤器返回一个“未定义”字段
- 如果我在 Filter() 函数中使用索引,我会得到一个未定义的引用错误吗?
- 另一个未定义的JavaScript问题
- 我得到一个未定义而不是文件名
- 为什么我的 push() 方法不断在我的数组中插入一个“未定义”的元素
- addEventListener 使用 for 循环给了我一个未定义的结果
- Promise.all 在完成之前返回一个未定义和解析的数组
- Angular JS给出了一个未定义的函数错误
- 我得到一个未定义的索引
- JS说我有一个未定义的对象.真的需要一些帮助来解决这个问题吗
- Ajax 调用给了我一个未定义 - 未定义的错误