如何从JSON响应中获取值

时间:2022-11-27 17:28:25

I get the following response from the server:

我从服务器收到如下回复:

{"aster":"3","daffodil":"4","rose":"3","totalItems":10,"totalPrice":"31.90"} RESPONSE

{“aster”:“3”,“水仙”:“4”,“玫瑰”:“3”,“totalItems”:10“totalPrice”:“31.90”}的回应

and, I want to put it in the table that will look like this:

我想把它放在这样的表格中

如何从JSON响应中获取值

The problem is that I do not know how to get values (for the "Quantity" column). This is my code:

问题是我不知道如何获取值(对于“Quantity”列)。这是我的代码:

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);
}

}

}

Before that, this is how my $.ajax looks like (it's bigger code, you don' have to read it out):

在此之前,这是我的$。ajax看起来像(它是更大的代码,你不需要读它):

$("#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);
        }
    });
})

I get the name from that response by value.name , but I have no idea how to get that value (quantity)?

我通过value。name获取响应的名称,但是我不知道如何获得这个值(数量)?

1 个解决方案

#1


1  

There is an error in your code.

您的代码中有一个错误。

It must be

它必须是

                cleanData.products.push({
                    name: prop,
                    quantity: primljeniOdgovor[prop]    //not data[prop]
                })

and not quantity: data[prop]. data is only an unparsed String object. primljeniOdgovor is the parsed Object that contains the property values.

而不是数量:数据(道具)。数据只是一个未解析的字符串对象。primljeniogovor是经过解析的对象,包含属性值。

See http://jsfiddle.net/t7n2tafk/

参见http://jsfiddle.net/t7n2tafk/

#1


1  

There is an error in your code.

您的代码中有一个错误。

It must be

它必须是

                cleanData.products.push({
                    name: prop,
                    quantity: primljeniOdgovor[prop]    //not data[prop]
                })

and not quantity: data[prop]. data is only an unparsed String object. primljeniOdgovor is the parsed Object that contains the property values.

而不是数量:数据(道具)。数据只是一个未解析的字符串对象。primljeniogovor是经过解析的对象,包含属性值。

See http://jsfiddle.net/t7n2tafk/

参见http://jsfiddle.net/t7n2tafk/