سؤال

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... ?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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.

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);
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/

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

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/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top