Muhammad Imran Tariq
Muhammad Imran Tariq

Reputation: 23352

Show Json data with dojo

I am building Dojo mobile app. I have a Json file like:

{
    Introduction: 
    [
        {
            title:   "Introduction",
            toolbar: "Page 1",
            content: "cont, aabitant morbi tristique..."
        },
        {
            title:   "Introduction",
            toolbar: "Page 2",
            content: "contesent vel nisi ipsum..."
        }
    ],
    Services: 
    [
        {
            title:   "services",
            toolbar: "Page 1",
            content: "Cras adipiscing sapien nec..."
        }
    ]
}

Following code prints introduction as written in title

dojo.xhrPost({
        url: "diet.json",
        handleAs: "json",
        load: function(response) {
            console.log(response.Introduction[0].title);
        }
    });

I am able to get inner data. How can I get first headings i.e.

Upvotes: 2

Views: 469

Answers (1)

Adam Rackis
Adam Rackis

Reputation: 83358

So you want the first title in each object in your response?

for (key in response)
    console.log(key + ": " + response[key][0].title);

Of course this assumes there's at least one element in each array in the response. If some may be empty, you'd want something like this:

for (key in response)
    console.log(key + ": " + 
                 (response[key].length > 0 ? response[key][0].title : "empty"));

Upvotes: 3

Related Questions