用foreach打印JSON数据
问题描述:
请问如何用foreach打印数据?用foreach打印JSON数据
我通常使用PHP对于这一点,但这里的服务器不包括PHP ...
感谢您的帮助。
{
"data": {
"items": [
{
"id": 1,
"status": "active",
"price": "300",
"size": {
"width": 12,
"height": 12
},
"english": {
"name": "My new item",
"description": "My item description"
}
},
{
"id": 2,
"status": "active",
"price": "560",
"size": {
"width": 11,
"height": 14
},
"english": {
"name": "My new item 2",
"description": "My paint item 2"
}
}
]
}
}
答
这取决于你的 '解析' 是什么意思。我建议你考虑一下这个问题,然后看看下面的例子。它使用一个JavaScript库jQuery,这是非常容易理解和广泛使用的。
function example()
{
var response = "";
var form_data = {
username: username,
password: password
};
$.ajax({
type: "POST",
url: base_url + "ajax.php?test/json",
data: form_data,
success: function(response)
{
/*response = '[{"Language":"jQuery","ID":"1"},{"Language":"C#","ID":"2"},
{"Language":"PHP","ID":"3"},{"Language":"Java","ID":"4"},
{"Language":"Python","ID":"5"},{"Language":"Perl","ID":"6"},
{"Language":"C++","ID":"7"},{"Language":"ASP","ID":"8"},
{"Language":"Ruby","ID":"9"}]'*/
console.log(response);
var json_obj = $.parseJSON(response);//parse JSON
var output="<ul>";
for (var i in json_obj)
{
output+="<li>" + json_obj[i].Language + ", " + json_obj[i].ID + "</li>";
}
output+="</ul>";
$('span').html(output);
},
dataType: "json"//set to JSON
})
}
+0
感谢您的帮助。我已经更新了这个问题。 – Tomas
答
我探头控制台
{
data = {
"items": [
{
"id": 1,
"status": "active",
"price": "300",
"size": {
"width": 12,
"height": 12
},
"english": {
"name": "My new item",
"description": "My item description"
}
},
{
"id": 2,
"status": "active",
"price": "560",
"size": {
"width": 11,
"height": 14
},
"english": {
"name": "My new item 2",
"description": "My paint item 2"
}
}
]
}
}
for(i=0; i< data.items.length; i++)
{
console.log('id: '+ data.items[i].id + ' , status: '+data.items[i].status+ ' , price: '+data.items[i].price)
console.log('width: '+data.items[i].size.width + ' , heigth: '+data.items[i].size.height);
console.log('name: '+data.items[i].english.name+ ' , Description: '+data.items[i].english. description);
}
我的工作
它已经被解析:d –
如何在foreach我可以打印DATAS好吗? – Tomas