I’ve seen many JQuery examples that make use of $.each to loop through a JSON array. However, what need to do is individually grab items 0 – 3 and pass them to another function called “Search”. Here’s what I’ve come up with.
$.getJSON("http://localhost:61741/binggame/play?cat=Body%20Parts", {
tags: "English",
tagmode: "any",
format: "json"
},
function (data) {
Search(data.items[0], "Box1_Image");
Search(data.items[1], "Box2_Image");
Search(data.items[2], "Box3_Image");
Search(data.items[3], "Box4_Image");
});
I’m fairly certain that data.items[] is not the correct syntax.
Here’s a sample of my JSON:
{"nouns":[
{
"ID":26,
"Category":"Body Parts",
"English":"muscle",
"Pinyin":"gei yUk",
"Image1":null,
"Audio1":null
},
{
"ID":27,
"Category":"Body Parts",
"English":"neck",
"Pinyin":"gen",
"Image1":null,
"Audio1":null
},
{
"ID":28,
"Category":"Body Parts",
"English":"nose",
"Pinyin":"bei",
"Image1":null,
"Audio1":null
},
{
"ID":29,
"Category":"Body Parts",
"English":"rib",
"Pinyin":"lat gwt",
"Image1":null,
"Audio1":null
}
]}
For this sample, the value of data.items[0] should be “muscle”, data.items[1] should be “neck”, data.items[2] should be “nose” and data.items[3] should be “rib”.
Can someone point out to me what I’ve done wrong?
To start, there is no property names
itemsin your JSON.If you want to get
"muscle":data.nouns[0].EnglishIf you want to get
"neck":data.nouns[1].Englishand so on:
or, to stay DRYer:
or better still: