2017-02-06 140 views
-1

我需要通过api以json格式的某些出租软件生成的选定数据创建表。所提供的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知识有限,这可能意味着我正在进行这种全部错误,或者没有足够好地描述它...... 任何帮助或改进的方法将不胜感激!

+0

你需要'这个[ 'rental_rate'] [ '价格']'或'this.rental_rate [ '价格']'或'this.rental_rate。价格“两分钟的基础研究会产生这些信息。 –

+0

@ChrisG - 是否可以倒推评论? – Adam

+0

@亚当号两分钟的基础研究将会产生这些信息。 –

回答

1

this.name让你的名字

this.rental_rate让你嵌套出租率的对象。

this.rental_rate.price给你的价格。

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

例如

this["name"]

this["rental_rate"]["price"]

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

+0

感谢Adam的快速反应,并解决了我的问题。我完全误解了我在做什么! – James