我從服務器得到如下回應:如何從JSON響應獲得價值
{ 「紫苑」: 「3」, 「水仙」: 「4」, 「玫瑰」: 「3」,」 TOTALITEMS 「:10,」 totalPrice 「:」 31.90" } RESPONSE
而且,我希望把它放在桌上,將是這樣的:
的問題是,我做的不知道如何獲取值(對於「數量」列)。這是我的代碼:
function processServerResponse(data) {
if (data.products.length > 0) {
$("#orderForm").hide();
$("#summaryForm").show();
var html = '';
$.each(data.products, function(key, value) {
html += "<tr><td>"+value.name+"</td><td>"+?????+"</td></tr>"
});
$(html).appendTo("tbody");
$("#totalItems").text(data.totalItems);
$("#totalPrice").text(data.totalPrice);
}
}
在此之前,這是我的$就怎麼樣子(這是更大的代碼,你不」已讀出來):
$("#orderForm button").click(function (e) {
e.preventDefault();
var formData = $("#orderForm").serialize();
$("#popup").show();
$("body *").not("#popup").css("opacity", 0.5);
$("input").prop("disabled", true);
$.ajax({
url: "http://localhost/",
type: "post",
data: formData,
dataType: "json",
dataFilter: function(data, dataType) {
primljeniOdgovor = $.parseJSON(data);
var cleanData = {
totalItems: primljeniOdgovor.totalItems,
totalPrice: primljeniOdgovor.totalPrice
};
delete primljeniOdgovor.totalItems;
delete primljeniOdgovor.totalPrice;
cleanData.products = [];
for (prop in primljeniOdgovor) {
cleanData.products.push({
name: prop,
quantity: data[prop]
})
}
return cleanData;
},
converters: {
"text json": function(data) {
return data;
}
},
success: function(data) {
processServerResponse(data);
},
complete: function() {
setTimeout(function() {
$("#popup").hide();
$("body *").not("#popup").css("opacity", 1);
$("input").prop("disabled", false);
}, 1500);
}
});
})
我通過value.name得到了該響應的名稱,但我不知道如何獲得該值(數量)?
'value.quantity' ?? – Brett 2015-02-10 19:53:07