我对jQuery非常陌生,并且正在尝试弄清楚如何将嵌套的JSON对象放入数组中。这是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"
}
}
}
从此JSON中,我需要price
,shippingCost.value
,shippingType
和title
。我已经搜索了实现此目标的方法,但是我发现的只是有关$ .getJSON()方法的信息,而且我不知道如何使用该方法来实现我的目标。如果有人能指出我正确的方向,将不胜感激!
答案 0 :(得分:0)
您可以使用$.getJSON
获取JSON,然后遍历其值并将其推入数组。
尝试使用此http://jsfiddle.net/j26bkomu/(打开控制台)
答案 1 :(得分:0)
一旦有了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);
});
输出:
(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)