簡體   English   中英

無法將嵌套的JSON對象推入數組

[英]Can't push nested JSON Objects into an array

我對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()方法的信息,而且我不知道如何使用該方法來實現我的目標。 如果有人能指出我正確的方向,將不勝感激!

您可以使用$.getJSON獲取JSON,然后遍歷它們的值並將其推入數組。

試試這個http://jsfiddle.net/j26bkomu/ (打開控制台)

擁有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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM