Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
150
rated 0 times [  152] [ 2]  / answers: 1 / hits: 88440  / 13 Years ago, tue, may 24, 2011, 12:00:00

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

More From » ajax

 Answers
80

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

}
}

[#92090] Sunday, May 22, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hailie

Total Points: 25
Total Questions: 112
Total Answers: 111

Location: Belize
Member since Tue, Dec 8, 2020
4 Years ago
;