开发者

Error at json: "missing ] after element list" or just "undefined"

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 开发者_StackOverflow社区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!


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"},
 // ...


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"}}


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)


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:

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)
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜