0

I have an object like this

var JSON_Object = {
"Diabites": [
    {
        "Day1": [
            {
                "Breakfast": [
                    {
                        "food1": "Bread",
                        "food2": "Tea",
                        "food3": "Edd"
                    }
                ]
            },
            {
                "Lunch": [
                    {
                        "food1": "Bread",
                        "food2": "Tea",
                        "food3": "Edd"
                    }
                ]
            },
            {
                "Dinner": [
                    {
                        "food1": "Bread",
                        "food2": "Tea",
                        "food3": "Edd"
                    }
                ]
            }
        ]
    },
    {
        "Day2": [
            {
                "Breakfast": [
                    {
                        "food1": "Bread",
                        "food2": "Tea",
                        "food3": "Edd"
                    }
                ]
            },
            {
                "Lunch": [
                    {
                        "food1": "Bread",
                        "food2": "Tea",
                        "food3": "Edd"
                    }
                ]
            },
            {
                "Dinner": [
                    {
                        "food1": "Bread",
                        "food2": "Tea",
                        "food3": "Edd"
                    }
                ]
            }
        ]
    }
]
}

Now how I can read this file using some loop to print out. I tried the following

alert(JSON_Object.Diabites.length); // Return 1 
alert(JSON_Object.Diabites[0].Day1.length); // Return 3

But there are different names like Day1, Day2, Day3 ...., so how can I change that part in the loop, like like

for(var i=0; i<JSON_Object.Diabites.length;i++)
{
    alert(JSON_Object.Diabites[i].Day1.length);
}

Update JSON Object

var JSON_Object = {
"Diabites": [
    {
        "Day1": [
            {
                "Breakfast": [
                    {
                        "food1": "Bread"
                    },
                    {
                        "food2": "Tea"
                    },
                    {
                        "food3": "Edd"
                    }
                ]
            },
            {
                "Lunch": [
                    {
                        "food1": "Bread"
                    },
                    {
                        "food2": "Tea"
                    },
                    {
                        "food3": "Edd"
                    }
                ]
            },
            {
                "Dinner": [
                    {
                        "food1": "Bread"
                    },
                    {
                        "food2": "Tea"
                    },
                    {
                        "food3": "Edd"
                    }
                ]
            }
        ]
    },
    {
        "Day2": [
            {
                "Breakfast": [
                    {
                        "food1": "Bread"
                    },
                    {
                        "food2": "Tea"
                    },
                    {
                        "food3": "Edd"
                    }
                ]
            },
            {
                "Lunch": [
                    {
                        "food1": "Bread"
                    },
                    {
                        "food2": "Tea"
                    },
                    {
                        "food3": "Edd"
                    }
                ]
            },
            {
                "Dinner": [
                    {
                        "food1": "Bread"
                    },
                    {
                        "food2": "Tea"
                    },
                    {
                        "food3": "Edd"
                    }
                ]
            }
        ]
    }
]
};
Blu
  • 4,036
  • 6
  • 38
  • 65
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) and [How do I enumerate the properties of a javascript object?](http://stackoverflow.com/q/85992/218196). – Felix Kling Dec 11 '13 at 05:53
  • 1
    Please note that the problem has **nothing** to do with JSON at all. It's rather about how to process arrays/objects in JavaScript. *How* you obtained the data (e.g. via JSON) is irrelevant to the problem. – Felix Kling Dec 11 '13 at 05:53

1 Answers1

1

If you want to loop over keys of a json, you can do like

for(var key in JSON_Object.Diabites[0]){

  //key will be Day1,Day2 etc.
  alert(JSON_Object.Diabites[0][key].length);

}

But in your json structure shared, you have to use

for(var i=0;i<JSON_Object.Diabites.length;i++) {

    for(var key in JSON_Object.Diabites[i]) {

      //key will be Day1,Day2 etc.
      alert(key);
      alert(JSON_Object.Diabites[i][key].length);

    }

}

here is a working fiddle if you are interested.

Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
  • Hey This is very helpful but i made a little mistake before in my json object now i updated my question can you take a look again? – Blu Dec 11 '13 at 06:45