如何打印jQuery的JSON数据?

时间:2018-07-23 06:29:06

标签: javascript jquery json

我有一些来自minicart.js的数据,当我使用console.log检查时,它显示如下值:

[Product, Product, Product, Product]
0:Product {_data: {…}, _options: Array(0), _discount: 0, _amount: 300, _total: 600, …}
1:Product {_data: {…}, _options: Array(0), _discount: 0, _amount: 150, _total: 150, …}
2:Product {_data: {…}, _options: Array(0), _discount: 0, _amount: 4500, _total: 4500, …}
3:Product {_data: {…}, _options: Array(0), _discount: 0, _amount: 400, _total: 2800, …

我正在使用的脚本如下:

<script>
paypalm.minicartk.render();
var items = paypalm.minicartk.cart.items();
console.log(items);
</script>

所以现在我想在HTML中显示amout和数据值。我在下面尝试过,但无法正常工作:

<script>
paypalm.minicartk.render();
var items = paypalm.minicartk.cart.items();
$("itemAmount").html('<span>+items.amount+</span>');
$("itemName").html('<span>+items.data.item_name+</span>');
</script>

注意:我的商品名称在“产品”下。

Product
 _amount: 300
 _data: 
    add: "1"
    amount:300
    cmd:"_cart"
    discount_amount:"0.00"
    href:"http://example.com/"
    item_name:"All Purpose Cream"
    quantity:2
    submit:"Add to cart"
 __proto__:Object
 _discount:0
 _eventCache:{change: Array(1)}
 _options:[]
 _total:600
 __proto__:Object

1 个答案:

答案 0 :(得分:1)

您可以对上述任务使用循环,如下所示:

<script>
paypalm.minicartk.render();
var items = paypalm.minicartk.cart.items();
for(var i=0;i<items.length;i++){
  $("#itemAmount").append('<span>'+items[i]._data.amount+'</span>');
  $("#itemName").append('<span>'+items[i]._data.item_name+'</span>');
}
</script>