Fahd
Fahd

Reputation: 353

help with accessing nodes at a json file in javascript

I need your help with the json structure !! (I know it's easy but I cann't figure what the error is).

in this code I'm having a json file that contains a list of items (say restaurants) along with some details for each branch, like the address. In my js file I'm trying to print all the addresses that match specific item id.

I will post a sample of the json file and the js code to access it.

here is the json file:

{
"items" : 
[

{
"item_id" :"204","details" :[{"country":"usa","city":"NY", "address":"bla bla bla"},{"country":"usa","city":"NY", "address":"another bla bla bla for the same id"}]
},
{
"item_id" :"234","details" :[{"country":"usa","city":"NY", "address":"another bla bla bla"}]
}

]
}

and here is the js code to access that file:

.....
success:function(data){
        $.each(data.items, function(i,item){
                if (item.item_id == 204 ) {
            $("#results").append("<hr/>");
                $("#results").append("<span class='result' >" + item.details.address + "</span><br/>");
    }
      });
    }, ....

but it doesn't print any thing.

What do you think?

Upvotes: 0

Views: 1238

Answers (3)

Robert
Robert

Reputation: 8767

Your JSON is valid, so that is not the problem.

{
    "items": [
        {
            "item_id": "204",
            "details": [
                {
                    "country": "usa",
                    "city": "NY",
                    "address": "bla bla bla"
                },
                {
                    "country": "usa",
                    "city": "NY",
                    "address": "another bla bla bla for the same id"
                }
            ]
        },
        {
            "item_id": "234",
            "details": [
                {
                    "country": "usa",
                    "city": "NY",
                    "address": "another bla bla bla"
                }
            ]
        }
    ]
}

You can view a modified example at: http://jsfiddle.net/wKDUu/. The code below will loop through the details to accomodate a more dynamic approach.

success:function(data){ 
    var di = data.items;     
    for(var i=0;i<di.length;i++){ 
        var ii = di[i]; 
        if (ii.item_id == 204 ) {              
            $("#results").append("<hr/>"); 
            for(var a=0;a<ii.details.length;a++){ 
                $("#results").append("<span class='result' >" + ii.details[a].address + "</span><br/>"); 
            } 
        }        
    }    
}),

Upvotes: 1

Benno
Benno

Reputation: 3008

You don't actually need to use $.each in this case, because you have an array of objects i.e. [{}, {}, {}] is an array of objects, even though you actually have an object of arrays of objects :p. Each object has a specified index i.e. "item" whereas each array of objects has a default index of 0,1,2 etc.

Also yeah, because you have an array of address object details, you need to access them via details[0] or details[1]

Check this example out on jsFiddle

for(var i =0;i<data.items.length;i++) {

if (data.items[i].item_id == "204" ) {
            $("#results").append("<hr/>");
                $("#results").append("<span class='result' >" + data.items[i].details[0].address + "</span><br/>");
    }
}

Upvotes: 1

Dennis
Dennis

Reputation: 32598

Since item.details is an array, you need to get it via item.details[0].address

Upvotes: 0

Related Questions