首页 > 解决方案 > 无法将嵌套的 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 我需要priceshippingCost.valueshippingTypetitle. 我已经搜索了执行此操作的方法,但我找到的只是有关 $.getJSON() 方法的信息,我不知道如何使用它来实现我的目标。如果有人能指出我正确的方向,将不胜感激!

标签: javascriptjson

解决方案


一旦你有了 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)

推荐阅读