I am new to JSON and trying to recover some text from a JSON variable.
The JSON variable is sent from a Javascript File and has this structure:
foo({
"query": {
"count": "2",
"created": "2009-07-25T08:17:54Z",
"lang": "en-US",
},
"results": {
"result": [
{
"abstract": "<b>Pizza</b> Hut®. Order Online for Delivery or Carry-out. Fast & Easy.",
"title": "<b>Pizza</b> Hut"
},
{
"abstract": "Official site of Domino's <b>Pizza</b> delivery chain, which offers thin crust, deep dish, and hand tossed <b>pizzas</b> with a variety of side items and beverages. Site <b>...</b>",
"title": "Domino's <b>Pizza</b>"
}
]
}
}
});
The JSON goes to a callback function named foo:
function foo(o){
var out = document.getElementById('container');
out.innerHTML = o.query.count;
}
My Problem: I know how to print out the query count variable using the callback function above, but I don't know how to print out the the title of the first result in the results array.
How can I change the callback function to display the first result title? And also, is there a foreach statement, where I could print out all the titles from all the results?
Thanks!
UPDATE: JSBIN for this code is at: http://jsbin.com/ejiwa/edit