I am stuck with a problem while parsing the response data in json format.
JSON Output from an external URL:
[
{
"id": "1",
"qtext": "Do you like this product?",
"op": [
{
"oid": "1",
"option": "option1"
},
{
"oid": "2",
"option": "option2"
},
{
"oid": "3",
"option": "option3"
},
{
"oid": "4",
"option": "option4"
}
]
},
{
"id": "16",
"qtext": "How is the quality of this video?",
"op": []
}
]
To parse this json, I have written this jQuery code:
$.ajax({
type: 'POST',
url: 'jsonexp1.php',
data: {id:1},
success: function(data) {
var pj = $.parseJSON(data);
$.each(pj,function(k,element){
$("#content").html($("#content").html()+"<p>"+element.id+" "+element.qtext);
});
}
});
I am able to read the first two items "id" and "qtext" from the json output. But, I am unable to traverse the other elements.
A guideline will help. Thanks
$.each()in your current$.each().