如何从多个对象级别获取json数据

时间:2017-02-06 19:26:30

标签: javascript json getjson

我需要通过api以json格式的某些租赁软件生成的选定数据创建一个表。提供的json数据是产品列表,对包含其他对象和数组的对象非常广泛。

当我需要从对象内的对象检索数据时,问题就出现了。我想从下面的数据中获取姓名,体重和租金价格。租赁价格在rental_rate对象中。

{
"products": [
{
  "id": 1404,
  "name": "Product 1",
  "type": "Product",
  "tag_list": [],
  "description": "",
  "allowed_stock_type": 1,
  "allowed_stock_type_name": "Rental",
  "stock_method": 1,
  "stock_method_name": "Bulk",
  "buffer_percent": "50.0",
  "post_rent_unavailability": 0,
  "replacement_charge": "0.0",
  "weight": "0.5",
  "barcode": "@st2078",
  "active": true,
  "accessory_only": false,
  "system": false,
  "discountable": true,
  "rental_rate": {
    "item_id": 1404,
    "store_id": 1,
    "transaction_type": 1,
    "rate_definition_id": 3,
    "price": "10.0",
    "deposit": "0.0",
    "category_prices": [],
    "properties": {
      "day_cost": "0.0",
      "day_price": "0.0",
      "hour_cost": "0.0",
      "hour_price": "0.0",
      "__value_types": "---\nday_price: BigDecimal\nhour_price: BigDecimal\ndistance_price: BigDecimal\nflat_rate_price: BigDecimal\nday_cost: BigDecimal\nhour_cost: BigDecimal\ndistance_cost: BigDecimal\nflat_rate_cost: BigDecimal\n",
      "distance_cost": "0.0",
      "distance_price": "0.0",
      "flat_rate_cost": "0.0",
      "flat_rate_price": "0.0"
    },
    "priority": 0,
    "date_range": "1900-01-01...3000-01-01"
  },
  "sale_rate": null,
  "product_group_id": 5,
  "tax_class_id": 2,
  "rental_revenue_group_id": 1,
  "sale_revenue_group_id": null,
  "created_at": "2017-02-06T00:49:47.755Z",
  "updated_at": "2017-02-06T00:49:47.755Z",
  "custom_fields": {
    "barcode_notes": "Actual Leg Length is: 3'",
    "product_serial_number": ""
  },
  "product_group": {
    "id": 5,
    "name": "Staging",
    "description": "",
    "created_at": "2017-02-05T22:20:53.465Z",
    "updated_at": "2017-02-05T22:20:53.465Z",
    "custom_fields": {}
  },
  "tax_class": {
    "id": 2,
    "name": "VAT Standard"
  },
  "icon": null,
  "rental_revenue_group": {
    "id": 1,
    "name": "Rental",
    "description": "",
    "active": true
  },
  "sale_revenue_group": null,
  "accessories": [],
  "alternative_products": [],
  "attachments": [],
  "rental_rates": [
    {
      "id": 1782,
      "store_id": null,
      "store_name": "",
      "rate_definition_id": 3,
      "rate_definition_name": "3 Day Week Rate",
      "starts_at": null,
      "ends_at": null,
      "price": "10.0",
      "category_prices": []
    }
  ],
  "sale_rates": []
},
{
  "id": 2395,
  "name": "Product 2",
  "type": "Product",
  "tag_list": [],
  "description": "",
  "allowed_stock_type": 1,
  "allowed_stock_type_name": "Rental",
  "stock_method": 2,x
  "stock_method_name": "Serialised",
  "buffer_percent": "50.0",
  "post_rent_unavailability": 0,
  "replacement_charge": "0.0",
  "weight": "45.0",
  "barcode": "",
  "active": true,
  "accessory_only": false,
  "system": false,
  "discountable": true,
  "rental_rate": {
    "item_id": 2395,
    "store_id": 1,
    "transaction_type": 1,
    "rate_definition_id": 3,
    "price": "0.0",
    "deposit": "0.0",
    "category_prices": [],
    "properties": {
      "day_cost": "0.0",
      "day_price": "0.0",
      "hour_cost": "0.0",
      "hour_price": "0.0",
      "__value_types": "---\nday_price: BigDecimal\nhour_price: BigDecimal\ndistance_price: BigDecimal\nflat_rate_price: BigDecimal\nday_cost: BigDecimal\nhour_cost: BigDecimal\ndistance_cost: BigDecimal\nflat_rate_cost: BigDecimal\n",
      "distance_cost": "0.0",
      "distance_price": "0.0",
      "flat_rate_cost": "0.0",
      "flat_rate_price": "0.0"
    },
    "priority": 0,
    "date_range": "1900-01-01...3000-01-01"
  },
  "sale_rate": null,
  "product_group_id": 6,
  "tax_class_id": 2,
  "rental_revenue_group_id": 1,
  "sale_revenue_group_id": null,
  "created_at": "2017-02-06T00:50:35.834Z",
  "updated_at": "2017-02-06T00:50:35.834Z",
  "custom_fields": {
    "barcode_notes": "",
    "product_serial_number": ""
  },
  "product_group": {
    "id": 6,
    "name": "Cases",
    "description": "",
    "created_at": "2017-02-05T22:20:53.509Z",
    "updated_at": "2017-02-05T22:20:53.509Z",
    "custom_fields": {}
  },
  "tax_class": {
    "id": 2,
    "name": "VAT Standard"
  },
  "icon": null,
  "rental_revenue_group": {
    "id": 1,
    "name": "Rental",
    "description": "",
    "active": true
  },
  "sale_revenue_group": null,
  "accessories": [],
  "alternative_products": [],
  "attachments": [],
  "rental_rates": [
    {
      "id": 2773,
      "store_id": null,
      "store_name": "",
      "rate_definition_id": 3,
      "rate_definition_name": "3 Day Week Rate",
      "starts_at": null,
      "ends_at": null,
      "price": "0.0",
      "category_prices": []
    }
  ],
  "sale_rates": []
}
  ],
 "meta": {
"total_row_count": 1376,
"row_count": 2,
"page": 1,
"per_page": 2
   }
  }

这是我用来获取数据的JS:

$(document).ready( function() {
$.getJSON( 'https://myapi.com/products', function(data) { 
  $.each(data.products, function() { 
     $("table#prod").append("<tr><td>" + this['name'] + "</td><td>" + this['rental_rate.price'] + "</td><td>" + this['weight'] + "kg</td></tr>");
  }); }); });

这适用于名称和重量,但不适用于租赁价格。我已经对互联网上的大多数地方进行了彻底搜索,但没有找到答案 - 我的javascript知识有限,这可能意味着我对这一切都错了,或者描述得不够好...... 任何帮助或改进的方法将不胜感激!

1 个答案:

答案 0 :(得分:1)

this.name为您提供名称

this.rental_rate为您提供嵌套租赁费用对象。

this.rental_rate.price为您提供价格。

您可以类似地使用square bracket notation,但人们通常只在点语法不起作用时才这样做(因为key中的空格)。

e.g。

this["name"]

this["rental_rate"]["price"]

this["Invalid Key when accessed via dot syntax but fine with square brackets"]