Pit Digger
Pit Digger

Reputation: 9800

Getting Data from a json sub array

I am looping through the following json. I get id and name fine, but when I do json.templates[i].dailyemails.length it always returns 0.

Here is my json:

{
   "templates":[
      {
         "id":"2c1d99d9b6b2fb417601d24c10c9b041a7d6f37b",
         "dailyemails":[
            "[email protected]",
            "[email protected]"
         ],
         "name":"Registration Report"             
      },
      {
         "id":"7d7cc642ca13cc4a998cad364dfe8e623fd95ae3",
         "dailyemails":[
            "[email protected]"
         ],
         "name":"Live Report"
      }

   ]
}

Upvotes: 0

Views: 15158

Answers (3)

Shyju
Shyju

Reputation: 218852

$.each(data.templates,function(index,item){
    alert(item.dailyemails.length)
});

Sample : http://jsfiddle.net/JbF29/2/

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039238

This should work just fine as seen in this live demo. Make sure that your actual JSON structure is the same as the one you have shown in your question.

Upvotes: 0

Denys Séguret
Denys Séguret

Reputation: 382314

If this is JSON, that is a string, you have to parse it and work with a javascript object :

var obj = JSON.parse(json);

And then you may query obj.templates[i].dailyemails

Upvotes: 1

Related Questions