循环遍历 JSON 解析循环遍历每个字符。
循环遍历 JSON 解析循环遍历每个字符。
我正在尝试循环遍历一个从PHP获取到的JSON字符串,但我遇到的问题是,当我尝试循环遍历字符串时,它并不会循环遍历每个对象,而是循环遍历字符串中的每个字符。
我以为解决方法是解析它,但并没有成功。
var json = JSON.stringify(player.get(url)); console.log(json); json = $.parseJSON(json); for (var key in json) { if (json.hasOwnProperty(key)) { console.log(key + " -> " + json[key]); } }
我得到了一个完美的JSON结果,因为我在在线转换器中进行了测试 -
{
"id": "1",
"username": "Jessica",
"password": "password",
"age": "100",
"size": "100"
}
然而,当我遍历它时,控制台显示如下:
0 -> { index.html:29
1 -> " index.html:29
2 -> 0 index.html:29
3 -> " index.html:29
4 -> : index.html:29
5 -> " index.html:29
6 -> 1 index.html:29
7 -> " index.html:29
8 -> , index.html:29
9 -> " index.html:29
10 -> c index.html:29
11 -> h index.html:29
12 -> a index.html:29
13 -> r
为什么它不能正确地循环遍历json对象呢?