2017-02-06 14 views
-1

いくつかのレンタルソフトウェアによって生成された選択されたデータから、JSON形式でAPIを介してテーブルを作成する必要があります。提供されたjsonデータは製品のリストであり、他のオブジェクトや配列を含むオブジェクトではかなり広範囲です。複数のオブジェクトレベルからjsonデータを取得する方法

問題は、オブジェクト内のオブジェクトからデータを取得する必要があるときに発生します。名前、重量、レンタル価格を以下のデータから取得したいと思います。レンタル料金は、rent_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を必要としています。 2分の基礎研究によってこの情報が得られたであろう。 –

+0

@ChrisG - コメントをdownvoteすることは可能ですか? – Adam

+0

@ Adam No.基本的な調査の2分でこの情報が得られました。 –

答えて

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

速い応答アダムと私の問題を解決してくれてありがとう。私は何をやっているのか全く間違っていた! – James

関連する問題