如何在JavaScript中循环php json编码数组? [关闭]
问题描述:
I get below result when I echo php json_encode function
[Object { item_id="18", name="Chocolate Cherry"}, Object { item_id="19", name="Spicy Mango "}]
How can loop through it?
当我回显php json_encode strong>函数时,我得到以下结果 p>
\ n 如何循环播放? p>
div>
[Object {item_id =“18”,name =“Chocolate Cherry”},Object {item_id =“19”,name =“Spicy Mango”}]
code> pre>
答
Try like this, simple javascript no jquery
var json = [{
"id" : "18",
"name" : "Chocolate Cherry"
},
{
"id" : "19",
"name" : "Spicy Mango"
}];
for (var key in json) {
if (json.hasOwnProperty(key)) {
alert(json[key].id);
alert(json[key].name);
}
}
</div>
答
jQuery.parseJSON() would help you.
答
Let's say your json string is str_json, At first parse it, using JSON.parse, after that you can loop it.
var arr_json = JSON.parse( str_json );
for( index in arr_json) {
alert( arr_json[index] )
}
答
Please try this out.
jsonobj = 'Your Json Object';
var jsontoarr = JSON.parse(jsonobj);
for( jsontoarr_each in jsontoarr) {
console.log(jsontoarr_each.item_id);
console.log(jsontoarr_each.name);
}
Will loop through the values.