json 错误:“元素列表后缺少 ]”或者只是“未定义”
我有一个像这样的 json:
{"156644":{ "name":"name1",
"street":"street1",
"city":"city1"
"68656":{ "name":"name2 ",
"street":"street2",
"city":"city1"
"388655":{ "name":"name3",
"street":"street3",
"city":"city1"
"4564":{ "name":"name4",
"street":"street4",
"city":"city1"
"6333":{ "name":"name5",
"street":"street5",
"city":"city1"}}
现在我想解码这个 json。在我的 Javascript 中我写道:
var object = eval(obj_json);
if(typeof(object)!="undefined"){
//fuel
for (var ii=0; ii<object.length; ii++){
alert(ii)
}
}
现在我在 object.length 处收到一个未定义的错误。所以我尝试像这样解析我的 json:
var object = eval(' (' + obj_json + ') '); alert(object.length);
但这根本不起作用。 现在我收到“元素列表后缺少]”错误。
你能帮我吗?请!
I have a json like:
{"156644":{ "name":"name1",
"street":"street1",
"city":"city1"
"68656":{ "name":"name2 ",
"street":"street2",
"city":"city1"
"388655":{ "name":"name3",
"street":"street3",
"city":"city1"
"4564":{ "name":"name4",
"street":"street4",
"city":"city1"
"6333":{ "name":"name5",
"street":"street5",
"city":"city1"}}
Now I want to decode this json. In my Javascript I wrote:
var object = eval(obj_json);
if(typeof(object)!="undefined"){
//fuel
for (var ii=0; ii<object.length; ii++){
alert(ii)
}
}
Now I get an Undefined error at object.length. So I tried to parse my json like:
var object = eval(' (' + obj_json + ') '); alert(object.length);
but this doesn't work at all.
Now I get an "missing ] after element list" error.
Can U help me? PLZ!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
每个元素后面缺少右括号:
相反,它可能应该如下所示:
You're missing closing brackets after each element:
Instead, it should probably look like:
您到处都缺少关闭
}
的东西。你不是想说这个吗:You're missing closing
}
thingies all over the place. Didn't you mean this:如果这是你的 JSON 的精确转储,那么你的问题出在你的 var 名称上;
您的 JSON 提供程序需要更改其输出,或者您需要在尝试解码之前对其进行解析,以在其中获取正确的变量名称:
json=eval( obj_json.replace( /([, \n\r\ t])([0-9]*):/g, '$1"v_$2":' ) );
(未经严格测试,只是一个适用于示例代码的示例)
If that is an exact dump of your JSON, then your problem is with your var names;
Your JSON provider needs to change their output, or you need to parse it before you try to decode it, to get proper var names in there:
json=eval( obj_json.replace( /([, \n\r\t])([0-9]*):/g, '$1"v_$2":' ) );
(not rigorously tested, just an example that works on the example code)
提出有关该问题的任何建议,因为OP的两个帖子内容不同。但仍然推荐 JSONLint。
Taking out any suggestions as to the problem, as the OP's two postings have different content. But still recommending JSONLint.
顺便说一句:我通过编码解决了这个问题:
现在它可以工作了!现在我可以写:
Btw: I fixed this Problem by coding:
now it works! Now I can write: