views:

916

answers:

7

Hello! 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!

+3  A: 

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"},
 // ...
John Feminella
+1  A: 

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"}}
Roatin Marth
A: 

The JSON does not seem to be formatted properly.

Salman A
A: 

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

kdgregory
OP did try your second `eval` example (see the bottom of the question).
Crescent Fresh
Thanks, didn't notice that. But I did just notice the real problem.
kdgregory
OP *is* using strings as keys. They just happen to contain numeric values :)
Crescent Fresh
Not in the second posting.
kdgregory
@kdgregory: ah, right. Still, JavaScript converts keys to strings. Eg `({ 12345:'foo' })["12345"]` still results in output `"foo"`.
Crescent Fresh
A: 

Sorry, I shortened the string a bit, cause there were several more data. The json is formatted properly!

Here is the json again:

({
    3263: {
        name: "name1",
        street: "street1",
        city: "city1"
    },
    3266: {
        name: "name2",
        street: "street2",
        city: "city2"
    },
    403073: {
        name: "name3",
        street: "street3",
        city: "city3"
    },
    19: {
        name: "name4",
        street: "street4",
        city: "city4"
    },
    3280: {
        name: "name5",
        street: "street5",
        city: "city5"        
    }
})

Thats how I get it. This is what I get in:

var object= eval(obj_json);
alert(object.toSource());
Newbie
Please edit your question to include this information.
Crescent Fresh
You're not trying to eval an object are you? This what you posted is *in a string* right?
Roatin Marth
obj_json is a string, right!
Newbie
A: 

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)

OhkaBaka
A: 

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