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

Javascript 如何从多个对象级别获取json数据,javascript,json,getjson,Javascript,Json,Getjson,我需要通过api从一些租赁软件生成的json格式的选定数据创建一个表。提供的json数据是一个产品列表,其中包含大量包含其他对象和数组的对象 当我需要从对象中的对象检索数据时,问题就出现了。我想从下面的数据中得到姓名、重量和租金。租金价格在租金率对象中 { "products": [ { "id": 1404, "name": "Product 1", "type": "Product", "tag_list": [], "description": "", "allow

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

当我需要从对象中的对象检索数据时,问题就出现了。我想从下面的数据中得到姓名、重量和租金。租金价格在租金率对象中

{
"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>");
  }); }); });
$(文档).ready(函数(){
$.getJSON('https://myapi.com/products,函数(数据){
$.each(data.products,function(){
$(“table#prod”)。追加(“+此['name']+”“+此['rental#u rate.price']+”“+此['weight']+“kg”);
}); }); });
这在名字和重量上都很好,但不是租金。我已经在互联网上的大多数地方做了彻底的搜索,但没有找到答案——我的javascript知识有限,这可能意味着我在这方面完全错了,或者描述得不够好。。。
任何帮助或改进方法都将不胜感激

this.name
获取名称

this.rental\u 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
   }
  }
这个.rent\u rate.price
可以为您提供价格

您也可以使用类似的方法,但人们通常只在点语法不起作用时才这样做(因为
键中有空格)

e、 g

this[“name”]

此[“租金”][“价格”]


this[“通过点语法访问时密钥无效,但可以用方括号括起来”]
此名称获取名称

this.rental\u 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
   }
  }
这个.rent\u rate.price
可以为您提供价格

您也可以使用类似的方法,但人们通常只在点语法不起作用时才这样做(因为
键中有空格)

e、 g

this[“name”]

此[“租金”][“价格”]


this[“通过点语法访问时密钥无效,但可以使用方括号”]

您需要
this['rental\u rate']['price']
this.rental\u rate['price']
这个.租金.价格
两分钟的基础研究会得出这个信息。@ChrisG-有可能否决评论吗?@Adam No.两分钟的基础研究会得出这个信息。谢谢你们,我想这是因为我不理解方括号的意思,所以我找错了问题。我现在知道更多。你需要
这个['price']['price']
这个.price['price']
这个.租金.价格
两分钟的基础研究会得出这个信息。@ChrisG-有可能否决评论吗?@Adam No.两分钟的基础研究会得出这个信息。谢谢你们,我想这是因为我不理解方括号的意思,所以我找错了问题。我现在知道更多了。谢谢你的快速反应,亚当,谢谢你解决了我的问题。我完全误解了我在做什么!谢谢你的快速回复,亚当,谢谢你解决了我的问题。我完全误解了我在做什么!