[英]Can't push nested JSON Objects into an array
I am very new to jQuery, and am trying to figure out how to put nested JSON Objects into an array. 我对jQuery非常陌生,并且正在尝试弄清楚如何将嵌套的JSON对象放入数组中。 Here is the JSON data:
这是JSON数据:
{
"0": {
"country": "US",
"itemId": "391296746967",
"price": "4.99",
"shippingCost": {
"expeditedShipping": "false",
"handlingTime": "1",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "Tea Infuser Ball Mesh Loose Leaf Herb Strainer Stainless Steel Secure Locking 2\"",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 4,
"page_number": 4
},
"search_terms": "ball"
}
},
"1": {
"country": "US",
"itemId": "382548457911",
"price": "18.9",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "1",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "Baby Kid Outdoor Indoor Princess Play Tent Playhouse Ball Pit Pool Toddler Toys",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 4,
"page_number": 4
},
"search_terms": "ball"
}
},
"2": {
"country": "US",
"itemId": "132543955826",
"price": "13.99",
"shippingCost": {
"expeditedShipping": "false",
"handlingTime": "1",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "Yoga Ball w Air Pump Anti Burst Exercise Balance Workout Stability 55 65 75 85cm",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 4,
"page_number": 4
},
"search_terms": "ball"
}
},
"3": {
"country": "US",
"itemId": "173659697373",
"price": "155.0",
"shippingCost": {
"expeditedShipping": "false",
"handlingTime": "0",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "DribbleUp Smart Soccer Ball with Training App Size 5 For Adults (***Buy ME***)",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 4,
"page_number": 4
},
"search_terms": "ball"
}
}
}
From this JSON I need the price
, the shippingCost.value
, the shippingType
, and the title
. 从这个JSON中,我需要
price
, shippingCost.value
, shippingType
和title
。 I have searched for ways to do this, but all I find is information about the $.getJSON() method, and I can't figure out how to use that to achieve my goal. 我已经搜索了实现此目标的方法,但是我发现的只是有关$ .getJSON()方法的信息,而且我不知道如何使用该方法来实现我的目标。 If anyone could point me in the right direction, it would be greatly appreciated!
如果有人能指出我正确的方向,将不胜感激!
You can get the JSON using $.getJSON
, then iterate over their values and push them into an array. 您可以使用
$.getJSON
获取JSON,然后遍历它们的值并将其推入数组。
Try this http://jsfiddle.net/j26bkomu/ (open the console) 试试这个http://jsfiddle.net/j26bkomu/ (打开控制台)
Once you have the json you can iterate over its key value pairs and save them in an array: 拥有json后,您可以遍历其键值对并将它们保存在数组中:
$.getJSON( "./test.json", function( data ) {
var items = [];
var objJson = {};
$.each( data, function( key, val ) {
objJson.price = val.price;
objJson.value = val.shippingCost.shippingServiceCost.value;
objJson.type = val.shippingCost.shippingType;
objJson.title = val.title;
items.push( objJson );
objJson = {};
});
console.log(items);
});
output: 输出:
(4) [{…}, {…}, {…}, {…}]
0:
price: "4.99"
title: "Tea Infuser Ball Mesh Loose Leaf Herb Strainer Stainless Steel Secure Locking 2""
type: "Free"
value: "0.0"
__proto__: Object
1:
price: "18.9"
title: "Baby Kid Outdoor Indoor Princess Play Tent Playhouse Ball Pit Pool Toddler Toys"
type: "Free"
value: "0.0"
__proto__: Object
2: {price: "13.99", value: "0.0", type: "Free", title: "Yoga Ball w Air Pump Anti Burst Exercise Balance Workout Stability 55 65 75 85cm"}
3: {price: "155.0", value: "0.0", type: "Free", title: "DribbleUp Smart Soccer Ball with Training App Size 5 For Adults (***Buy ME***)"}
length: 4
__proto__: Array(0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.