1

i have below json which i get from Remote.

{

    "name":"planning Diet",
    "day1":{
        "morning":"chicken",
        "evening":"mutton",
        "night":"Juice"
    },
    "day2":{
        "morning":"burger",
        "evening":"pizza",
        "night":"Juice",
        "late night":"water"
    }

}

Below is what i tried.

$.getJSON("http://domain.com/hello.json",function(result){
  $.each(result, function(i, field)
{
console.log(field);
});

This returns something like

"planning diet"
[object object]
[object object]

Now how can i traverse or loop through all the object... ?

2
  • @oGeez : i need to add all those items in my html elements ! Commented Feb 4, 2014 at 11:11
  • @Cerbrus : that url have answers which is really complicated ! i am expecting some easy solution. Commented Feb 4, 2014 at 11:14

6 Answers 6

3

In your case you can use something like this,

$.getJSON("http://domain.com/hello.json", function(result) {
    $.each(result, function(i, field) {
        if (typeof field == "object") {
            console.log(i);  
            $.each(field, function(i, f) {
                console.log(f);
            });
        }
        else {
            console.log(field);
        }
    });
});

You can use typeof field == "object" to check the current item is an object or not

Sign up to request clarification or add additional context in comments.

10 Comments

perfect ! will accept soon
hey ! how can i get object name ? that is day1 and day 2 ?
use the i variable inside if condition
|
1

You need a function with recursive loop as below:

function getValues(obj) {
    if(typeof obj== "object") {
        $.each(obj, function(key,value) {
            getValues(value);
        });
    }
    else {
       console.log(obj);
    } 
}

Here you are basically doing this:

1) Iterate over the json item, check if item is an object. 
2) If so,iterate over this new object, else print the value. 
3) This goes on till all the values are printed and whole json structure is iterated.

The parameter to the function should be json which you get from remote location.

Comments

0
var traverse = function(result){
    $.each(result, function(i, field) {
        if ($.isPlainObject(field)) {
            traverse(field);
        } else {
            console.log(field);
        }
    });
}
$.getJSON("http://domain.com/hello.json", traverse);

Comments

0
function traverse(obj){
   for(prop in obj){
       if(typeof obj[prop] === "object"){
           traverse(obj[prop]);
       }else{
          console.log(obj[prop]);
       }
   }
}

JS Fiddle: http://jsfiddle.net/Rbs9A/

Comments

0

You need to loop with $.each() only when you have got [{},{},{}...] multiple objects array, but in your case you just have a object which holds another object, so you don't have to do any iteration in a loop. You can refer with its keys:

$.getJSON("http://domain.com/hello.json",function(result){
   console.log(result); // gives you your object
   console.log(result.name); // gives you you "planning Diet"

   console.log(result.day1.morning); // gives you you "chicken"
});

Comments

0

Try this, field.day1.morning

data=[{

    "name":"planning Diet",
    "day1":{
        "morning":"chicken",
        "evening":"mutton",
        "night":"Juice"
    },
    "day2":{
        "morning":"burger",
        "evening":"pizza",
        "night":"Juice",
        "late night":"water"
    }

}]
$.each(data, function(i, field){
alert(field.day1.morning)
})

Demo http://jsfiddle.net/Aj9eJ/

1 Comment

no this is not traverse..its just getting particular item from json

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.