I get below result when I echo php json_encode function
当我回显php json_encode函数时,我得到了以下结果
[Object { item_id="18", name="Chocolate Cherry"}, Object { item_id="19", name="Spicy Mango "}]
How can loop through it?
如何循环呢?
4 个解决方案
#1
Try like this, simple javascript no jquery
试试这样,简单的javascript没有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);
}
}
#2
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.
将遍历值。
#3
jQuery.parseJSON() would help you.
jQuery.parseJSON()会帮助你。
#4
Let's say your json string is str_json, At first parse it, using JSON.parse, after that you can loop it.
假设您的json字符串是str_json,首先使用JSON.parse解析它,之后您可以循环它。
var arr_json = JSON.parse( str_json );
for( index in arr_json) {
alert( arr_json[index] )
}
#1
Try like this, simple javascript no jquery
试试这样,简单的javascript没有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);
}
}
#2
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.
将遍历值。
#3
jQuery.parseJSON() would help you.
jQuery.parseJSON()会帮助你。
#4
Let's say your json string is str_json, At first parse it, using JSON.parse, after that you can loop it.
假设您的json字符串是str_json,首先使用JSON.parse解析它,之后您可以循环它。
var arr_json = JSON.parse( str_json );
for( index in arr_json) {
alert( arr_json[index] )
}