get JSON reading multidimensional JSON array in PHP

2013 年 6 月 28 日5600

I'm using getJSON to retrieve information from a record and the corresponding detail records and display the detail records in a div. The PHP script that is called returns a JSON string (via json_encode) that looks like this:

{
"content": {
"1": {
"quadrant": "Balance",
"text": "1. Meditative or prayerful activity today?",
"answers": [
{
"answer_id": "4",
"text": "None",
"score_value": "1"
},
{
"answer_id": "5",
"text": "Few",
"score_value": "2"
},
{
"answer_id": "6",
"text": "Lots",
"score_value": "3"
}
]
},
"2": {
"quadrant": "Activity",
"text": "3. How much exercise (min.)?",
"answers": [
{
"answer_id": "1",
"text": "<15",
"score_value": "1"
},
{
"answer_id": "2",
"text": "15-30",
"score_value": "2"
},
{
"answer_id": "3",
"text": "30+",
"score_value": "3"
}
]
}
}
}

I have used getJSON as,

<script type="text/javascript">
$.getJSON('questions.php?callback=?', function (data) {
$("#questions").html('');
$.each(data.content, function (i, item) {
$.each(item, function(index, obj){
$("#questions").append('<div><p>'+ obj.text +'</p></div><hr />');
})
});
});
</script>

We must display the HTML div like below,

<div>
<p>1. Meditative or prayerful activity today?</p>
<fieldset>
<div><label>
<input type="checkbox">NONE
</label></div>
<div><label>
<input type="checkbox">FEW
</label></div>
<div><label>
<input type="checkbox">LOTS
</label></div>
</fieldset>
</div>
<hr />

<div>
<p>2. how was your atress level?</p>
<fieldset>
<div><label>
<input type="checkbox">HIGH
</label></div>
<div><label>
<input type="checkbox">MEDIUM
</label></div>
<div><label>
<input type="checkbox">LOW
</label></div>
</fieldset>
</div>
<hr />

My problem comes in the getJSON section where I'm trying to populate the table with a record. I don't know how to refer to the different fields in array 'content'. I've tried many variations.

How do I refer to a nested field in a multidimensional array like this?

0 0