javascript - 无法将嵌套的 JSON 对象推送到数组中
问题描述
我对 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() 方法的信息,我不知道如何使用它来实现我的目标。如果有人能指出我正确的方向,将不胜感激!
解决方案
一旦你有了 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)
推荐阅读
- javascript - 如何引用 D3 父元素的类?
- python-3.x - 与 conda 一起安装的 lxml:“ImportError:DLL 加载失败:找不到指定的过程”
- visual-studio - 二进制文件添加到 /bin 时没有引用,为什么?
- sql-server - MSSQL 选择与当前列具有相同列值的以下(序列)行等于 X
- r - 将 for 循环重写为应用程序,除了许多循环之外,它没有一组预定义的值可以循环
- jquery - jQuery Ajax 完成事件响应为空
- d3.js - 带有月份名称的nvd3折线图xaxis不起作用
- r - R根据另一列按组设置值
- angular - Angular CLI 版本和 Angular Core 版本之间的兼容性?
- c# - 动态加载 dll 时找不到由 URI '/mydll;component/subfolder/mainwindow.xaml' 标识的资源