jQuery返回键值[object object]

时间:2018-04-19 02:32:22

标签: jquery

我还是jQuery的新手。当我这样做时,我有一些问题:

console.log(data);
var a = JSON.parse(data);
//console.log(a);
var prodName = a.productName; 
var splitProductName = "";
var prodQty = a.quota; 
var splitProductQuota = "";
var prodPrice = a.price;
jQuery.each(prodName, function(index1, value1) {
    splitProductName = value1;
  contents += "<tr>";
  contents += '<td>' + splitProductName;
  jQuery.each(prodQty, function(index, value) {
        if (index1 == index) {
        splitProductQuota = value;
        contents += '</td><td>' + splitProductQuota + '</td>' + '<td>' + '' + '</td>' + '<td>' + '' + '</td>' + '<td>' + '' + '</td>';
        contents += "</tr>";
        // jQuery.each(prodPrice, function(index2, value2){
        //    alert(price_value_with_tax.value2);
        // });
        jQuery.each(prodPrice, function(index2, value2) {
        alert(price_value_with_tax.value2);
        });
    }
    });
});

这是在控制台上返回的数据结果(代码的第一行)。

{"quota":["14"],"price":[[{"price_id":"129","price_currency_id":"96","price_product_id":"127","price_value":"3.25000","price_min_quantity":"0","price_access":"all","price_users":"","price_site_id":"","price_start_date":"0","price_end_date":"0","price_value_with_tax":3.444999999999999840127884453977458178997039794921875,"taxes_added":true,"taxes":{"0000_00000003":{"taxation_id":"3","zone_namekey":"","category_namekey":"default_tax","tax_namekey":"GST","taxation_published":"1","taxation_type":"","taxation_access":"all","taxation_cumulative":"0","taxation_post_code":"","taxation_date_start":"0","taxation_date_end":"0","taxation_internal_code":"","taxation_note":"GST Tax Malaysia","taxation_site_id":"","taxation_ordering":"0","tax_rate":"0.06000","tax_ratio":1,"tax_amount":0.195000000000000006661338147750939242541790008544921875}}}]],"productName":["Baked Beans 230g"]}

但是当我想要获得 price_value_with_tax时,它会返回undefined 。如何设置代码来获取 price_value_with_tax 的值? 感谢先进的SO。

2 个答案:

答案 0 :(得分:1)

试试这个

var a = JSON.parse(data);
console.log(a);
var prodPrice = a.price;
jQuery.each(prodPrice, function(index2, value2){
    jQuery.each(value2, function(priceIndex, priceArr){
        console.log(priceArr.price_value_with_tax);
    });
});

See attachment, object ref.

答案 1 :(得分:0)

您需要在JSON数据中正确引用price_value_with_tax属性。

根据您的样本,该物业位于以下位置:

alert("value: " + a.price[0][0].price_value_with_tax);

假设您的示例数据只是一个片段,您可能希望在某些嵌套循环中提取这些值,以迭代价格键中的2个数组。