5

From the below JSON, how can I retrieve title from the note and notes using a for loop and ajax to retrieve?

{
"infos": {
        "info": [
        {
            "startYear": "1900",
            "endYear": "1930",
            "timeZoneDesc": "daweerrewereopreproewropewredfkfdufssfsfsfsfrerewrBlahhhhh..",
            "timeZoneID": "1",
                            "note": {
                "notes": [
                    {
                        "id": "1",
                        "title": "Mmm"
                    },
                    {
                        "id": "2",
                        "title": "Wmm"
                    },
                    {
                        "id": "3",
                        "title": "Smm"
                    }
                ]
            },
            "links": [
                { "id": "1", "title": "Red House", "url": "http://infopedia.nl.sg/articles/SIP_611_2004-12-24.html" },
                { "id": "2", "title": "Joo Chiat", "url": "http://www.the-inncrowd.com/joochiat.htm" },
                { "id": "3", "title": "Bake", "url": "https://thelongnwindingroad.wordpress.com/tag/red-house-bakery" }
            ]
        }

I tried out the code below but it doesn't work - it either says:

is null

not an object

length is null

r not an object

var detail = eval(xmlhttprequest.responseText)
var rss = detail.infos.info
for(var i = 0; i<rss.length; i++)
   startyear += rss[i].startyear
4
  • i answered similar question some day before, read this stackoverflow.com/questions/6081062/… Commented May 24, 2011 at 7:29
  • @diEcho not really the same...cause the json data that this article is there no array within the array Commented May 24, 2011 at 7:59
  • Where in the code you posted are you trying to access the title? What is i? Commented May 24, 2011 at 8:14
  • i don't how to access the title, i want to access the title in the json data i given above Commented May 24, 2011 at 8:19

4 Answers 4

6

Use

for (i = 0; i < 3; i++) {
    alert(JSON.infos.info[0].note.notes[i].title);
}

TRY IT HERE: JSFIDDLE WORKING EXAMPLE

BTW your JSON is not valid. Use this JSON:

var JSON = {
    "infos": {
        "info": [
            {
                "startYear": "1900",
                "endYear": "1930",
                "timeZoneDesc": "daweerrewereopreproewropewredfkfdufssfsfsfsfrerewrBlahhhhh..",
                "timeZoneID": "1",
                "note": {
                    "notes": [
                        {
                            "id": "1",
                            "title": "Mmm"
                        },
                        {
                            "id": "2",
                            "title": "Wmm"
                        },
                        {
                            "id": "3",
                            "title": "Smm"
                        }
                    ]
                },
                "links": [
                    {
                        "id": "1",
                        "title": "Red House",
                        "url": "http://infopedia.nl.sg/articles/SIP_611_2004-12-24.html"
                    },
                    {
                        "id": "2",
                        "title": "Joo Chiat",
                        "url": "http://www.the-inncrowd.com/joochiat.htm"
                    },
                    {
                        "id": "3",
                        "title": "Bake",
                        "url": "https://thelongnwindingroad.wordpress.com/tag/red-house-bakery"
                    }
                ]
            }
        ]
    }
}

EDIT:

Here is what you want:

var infoLength= JSON.infos.info.length;

for (infoIndex = 0; infoIndex < infoLength; infoIndex++) {

    var notesLength= JSON.infos.info[infoIndex].note.notes.length;

    for (noteIndex = 0; noteIndex < notesLength; noteIndex++) {

        alert(JSON.infos.info[infoIndex].note.notes[noteIndex].title);

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

11 Comments

my json data is put in the json file so i must retrieve from the file...the var JSON = eval(xmlhttprequest.responseText) ...does it work the same..cause i got to use something like this
var JSON = eval ( xmlhttp.responseText ); yeah it works the same. Just be sure the response is valid and parse-able.
@krmby if i want to loop within the length instead of what you show me the number 3, how should i do?
@krmby it say that JSON.infos.info.note.notes.length is null or not an object
@Cutex: Because info is an array, not an object. You have to access it with an index.
|
2

Putting your json into an var called obj, use the following:

obj.infos.info[0].note.notes[0].title

http://jsfiddle.net/Znq34/

1 Comment

i try something like this var detail = eval.... then for loop it inside should be var start year="" startyear += ...[i].startyear...then how should do it for the title, the obj will change right for what you given me
2

Well the "path" to the JSON notes array-like object is:

json.infos.info[0].note.notes;

So you could do something like:

var notes = json.infos.info[0].note.notes;
var titles = [];
for (var i = 0, len = notes.length; i < len; i++)
{
   titles.push(notes[i].title);
}

alert('titles is: ' + titles.join(', '));

Fiddle: http://jsfiddle.net/garreh/uDxqD/


Are you using jQuery? ;-)

// Assuming your using "success" in ajax response
success: function(json)
{
    var titles = $(json.infos.info[0].note.notes).map(function() {
        return this.title;
    }).get();
    alert(titles.join(', '));
}

4 Comments

No the question is, where did YOU get tat json.
no i not using jquery, i also never use push?where do you get the json from??
@Gary that json i have to retrieve from the ajax and in the json data file...so basically i must retrieve from the json file and put retrieve the title of the note array? i try this way var detail = eval.... then for loop it inside should be var start year="" startyear += ...[i].startyear then this way but the title in the note array don't
@Cutex: What is your problem now? The answer should work fine. json is just the name of the variable that contains the data. You have to use the name you use in your implementation.
0

First count the length of notes

var len = jsonobject.infos.info.note.notes.length;

Then loops through and get

var title = jsonobject.infos.info.note.notes[i].title;

2 Comments

can show me more specific? which do you get the jsonobject? is it from the var jsonobject = eval(xmlhttprequest.responseText)...do you get your jsonobject from here?
I simply tried var jsonobject = eval(your json); It is same if you are getting that json from Ajax response like var jsonobject = eval(xmlhttprequest.responseText); In any case you have to create json object and then use as I explained before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.