json 错误:“元素列表后缺少 ]”或者只是“未定义”

发布于 2024-08-11 21:46:36 字数 948 浏览 4 评论 0原文

我有一个像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光沙漏 2024-08-18 21:46:36

每个元素后面缺少右括号:

 {"156644":{ "name":"name1",
           "street":"street1",
           "city":"city1"       // Bracket needs to be here!
 "68656":{  "name":"name2 ",
           "street":"street2",
           "city":"city1"       // Bracket needs to be here!
// ...

相反,它可能应该如下所示:

 {"156644":{ "name":"name1",
           "street":"street1",
           "city":"city1"},
 "68656":{  "name":"name2 ",
           "street":"street2",
           "city":"city1"},
 // ...

You're missing closing brackets after each element:

 {"156644":{ "name":"name1",
           "street":"street1",
           "city":"city1"       // Bracket needs to be here!
 "68656":{  "name":"name2 ",
           "street":"street2",
           "city":"city1"       // Bracket needs to be here!
// ...

Instead, it should probably look like:

 {"156644":{ "name":"name1",
           "street":"street1",
           "city":"city1"},
 "68656":{  "name":"name2 ",
           "street":"street2",
           "city":"city1"},
 // ...
伴梦长久 2024-08-18 21:46:36

您到处都缺少关闭 } 的东西。你不是想说这个吗:

 {"156644":{ "name":"name1",
           "street":"street1",
           "city":"city1" }, // THIS, RIGHT HERE
 "68656":{  "name":"name2 ",
           "street":"street2",
           "city":"city1" }, // AND THIS
 "388655":{ "name":"name3",
           "street":"street3",
           "city":"city1" }, // AND THIS
 "4564":{  "name":"name4",
           "street":"street4",
           "city":"city1" }, // AND THIS
 "6333":{  "name":"name5",
           "street":"street5",
           "city":"city1"}}

You're missing closing } thingies all over the place. Didn't you mean this:

 {"156644":{ "name":"name1",
           "street":"street1",
           "city":"city1" }, // THIS, RIGHT HERE
 "68656":{  "name":"name2 ",
           "street":"street2",
           "city":"city1" }, // AND THIS
 "388655":{ "name":"name3",
           "street":"street3",
           "city":"city1" }, // AND THIS
 "4564":{  "name":"name4",
           "street":"street4",
           "city":"city1" }, // AND THIS
 "6333":{  "name":"name5",
           "street":"street5",
           "city":"city1"}}
一人独醉 2024-08-18 21:46:36

如果这是你的 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)

绝情姑娘 2024-08-18 21:46:36

提出有关该问题的任何建议,因为OP的两个帖子内容不同。但仍然推荐 JSONLint

Taking out any suggestions as to the problem, as the OP's two postings have different content. But still recommending JSONLint.

走过海棠暮 2024-08-18 21:46:36

顺便说一句:我通过编码解决了这个问题:

eval("var jsonobject=" + obj_json);

现在它可以工作了!现在我可以写:

for(ii in jsonobject){
    var intii = parseInt(ii);
    if (!isNaN(intii)){
        var street = jsonobject[ii].street;
        alert(street)
    }
}

Btw: I fixed this Problem by coding:

eval("var jsonobject=" + obj_json);

now it works! Now I can write:

for(ii in jsonobject){
    var intii = parseInt(ii);
    if (!isNaN(intii)){
        var street = jsonobject[ii].street;
        alert(street)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文