我尝试将我的字符串传递给
{" 1" {" 11" {" cityid":1," BPID":11,"名称":" Golden Mile 塔""时间":" 00:00"" sbpid":50043}}," 2":{& #34; 34" {" cityid":2" BPID":34,"名称":" KL 中环""时间":" 00:00"" sbpid":50040}}," 8":{& #34; 244" {" cityid":8," BPID" 244"名称":"塔曼 Sri Puteri"," time":" +00:00"," sbpid":50042}}}
console.log(JSON.parse(str))
它不会成为我期望的
知道为什么吗?
答案 0 :(得分:1)
它没有产生正确的阵列,因为你没有它。
您有Object
而非Array
这是数组,因为它从索引1开始:
{
"1":{"11":{"cityid":1,"bpid":11,"name":"Golden Mile Tower","time":"+00:00","sbpid":50043}},
"2":{"34":{"cityid":2,"bpid":34,"name":"KL Sentral","time":"+00:00","sbpid":50040}},
"8":{"244":{"cityid":8,"bpid":244,"name":"Taman Sri Puteri","time":"+00:00","sbpid":50042}}
}
这是数组:
[
{"11":{"cityid":1,"bpid":11,"name":"Golden Mile Tower","time":"+00:00","sbpid":50043}},
{"34":{"cityid":2,"bpid":34,"name":"KL Sentral","time":"+00:00","sbpid":50040}},
{"244":{"cityid":8,"bpid":244,"name":"Taman Sri Puteri","time":"+00:00","sbpid":50042}}
]
要将Object
转换为Array
,请使用循环,例如:
var arr = [];
for( var a in your_object ){
arr.push(a);
}
答案 1 :(得分:0)
JSON.parse只解析字符串,因此您提供的对象因此不会解析对象。所以用单引号包装你的对象。
我在开头和最后添加了单引号。现在你可以检查一下。它将返回数组。
JSON.parse('{“1”:{“11”:{“cityid”:1,“bpid”:11,“name”:“Golden Mile Tower”,“time”:“+ 00:00 “,”sbpid“:50043}},”2“:{”34“:{”cityid“:2,”bpid“:34,”name“:”KL Sentral“,”time“:”+ 00:00 “,”sbpid“:50040}},”8“:{”244“:{”cityid“:8,”bpid“:244,”name“:”Taman Sri Puteri“,”time“:”+ 00: 00" , “sbpid”:50042}}}“);