【发布时间】:2017-05-15 16:26:35
【问题描述】:
这是 Postman 返回的 JSON 格式:
e[{
"OrderID": 10248,
"CustomerID": "VINET",
"ContactName": "Paul Henriot",
"EmployeeID": 5,
"OrderDate": "/Date(836431200000)/",
"RequiredDate": "/Date(838850400000)/",
"ShippedDate": "/Date(837468000000)/",
"ShipVia": 3,
"Freight": 32.38,
"ShipName": "Vins et alcools Chevalier",
"ShipAddress": "59 rue de l'Abbaye",
"ShipCity": "Reims",
"ShipRegion": null,
"ShipPostalCode": "51100",
"ShipCountry": "France",
"ProductID": 11,
"ProductName": "Queso Cabrales",
"UnitPrice": 14,
"Quantity": 12,
"Discount": 0},
{
"OrderID": 10248,
"CustomerID": "VINET",
"ContactName": "Paul Henriot",
"EmployeeID": 5,................
如何使用 Javascript 循环来迭代这个数组,以便为每个 ORDER ID 显示产品 ID、产品名称、单价、折扣?我试过这个:
e function format(d)
{
var jqxhr = $.getJSON("/Test/GetData", function (jsonResult)
{
ParseJson(jsonResult);
})
function ParseJson(response)
{
var html = "";
for (var i = 0; i < response.length; i++)
{
var id = response[i].OrderID;
html += "<tr>";
html += "<td>" + response[i].ProductID + "</td>";
html += "<td>" + response[i].ProductName + "</td>";
html += "<td>" + response[i].UnitPrice + "</td>";
html += "<td>" + response[i].Quantity + "</td>";
html += "<td>" + response[i].Discount + "</td>";
html += "</tr>";
}
$(document).find("#entrydata").html(html);
}
return '<table id = "entrydata" cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">' +
'</table>';
}
但此代码会针对每个订单 ID 返回数据库中的所有产品详细信息。
【问题讨论】:
-
据我所知,这与 C# 完全无关。
标签: javascript arrays json postman