簡體   English   中英

Json回應。 使用javascript或php檢索特定值

[英]Json response. Retrieve specific values with javascript or php

我有一個在shopify中工作的API,當我使用特定任務的url時,它會在JSON中返回一個響應。 瀏覽器將其翻譯為RAW。 但是,我的問題是如何從JSON響應中檢索特定值? 我想只使用javascript或php腳本提取“屬性”部分。 下面的代碼不是file.json,它是我調用特定URL時服務器的響應。 mysite.myshopify.com/admin/orders.json?fields=line_items

JSON響應示例:

{
    orders: [50]
    0:  {
        name: "#1347"
        line_items: [1]
        0:  {
            fulfillment_service: "manual"
            fulfillment_status: null
            gift_card: false
            grams: 0
            id: 966685828
            price: "45.00"
            product_id: 455951420
            quantity: 1
            requires_shipping: true
            sku: ""
            taxable: true
            title: "Athletic Style Auto renew (ships every 1 Months)"
            variant_id: 1292559264
            variant_title: ""
            vendor: null
            name: "Athletic Style Auto renew (ships every 1 Months)"
            variant_inventory_management: null
            properties: [9]
            0:  {
                name: "Glove Size"
                value: "M"
            }-
            1:  {
                name: "Hat Size"
                value: "L/XL"
            }-
            2:  {
                name: "Pant Size"
                value: "30x30"
            }-
            3:  {
                name: "Right or Left Handed?"
                value: "Right"
            }-
            4:  {
                name: "Shirt Size"
                value: "M"
            }-
            5:  {
                name: "Shoe Size"
                value: "9"
            }-
            6:  {
                name: "shipping_interval_frequency"
                value: "1"
            }-
            7:  {
                name: "shipping_interval_unit_type"
                value: "Months"
            }-
            8:  {
                name: "subscription_id"
                value: "1522"
            }-
            -
            product_exists: true
            fulfillable_quantity: 1
            total_discount: "0.00"
            tax_lines: [0]
        }-
    -
    }
} 

 1: { line_items: [1] 0: { fulfillment_service: "manual" fulfillment_status: null gift_card: false grams: 0 id: 978288644 price: "45.00" product_id: 449447992 quantity: 1 requires_shipping: true sku: "" taxable: true title: "Loud and Wild Style Auto renew (ships every 1 Months)" variant_id: 1253803928 variant_title: "" vendor: null name: "Loud and Wild Style Auto renew (ships every 1 Months)" variant_inventory_management: null properties: [9] 0: { name: "Glove Size" value: "XL" }- 1: { name: "Hat Size" value: "L/XL" }- 2: { name: "Pant Size" value: "44x30" }- 3: { name: "Right or Left Handed?" value: "Left" }- 4: { name: "Shirt Size" value: "XXL" }- 5: { name: "Shoe Size" value: "10.5" }- 6: { name: "shipping_interval_frequency" value: "1" }- 7: { name: "shipping_interval_unit_type" value: "Months" }- 8: { name: "subscription_id" value: "1523" }- - product_exists: true fulfillable_quantity: 1 total_discount: "0.00" tax_lines: [0] }- - }- 2: {...}- 3: { line_items: [1] 0: { fulfillment_service: "manual" fulfillment_status: null gift_card: false grams: 0 id: 974181252 price: "45.00" product_id: 455951420 quantity: 1 requires_shipping: true sku: "" taxable: true title: "Athletic Style Auto renew (ships every 1 Months)" variant_id: 1292559264 variant_title: "" vendor: null name: "Athletic Style Auto renew (ships every 1 Months)" variant_inventory_management: null properties: [9] 0: { name: "Glove Size" value: "XL" }- 1: { name: "Hat Size" value: "L/XL" }- 2: { name: "Pant Size" value: "42x30" }- 3: { name: "Right or Left Handed?" value: "Right" }- 4: { name: "Shirt Size" value: "XXL" }- 5: { name: "Shoe Size" value: "12" }- 6: { name: "shipping_interval_frequency" value: "1" }- 7: { name: "shipping_interval_unit_type" value: "Months" }- 8: { name: "subscription_id" value: "1522" }- - product_exists: true fulfillable_quantity: 1 total_discount: "0.00" tax_lines: [0] }- - } 

正如人們在評論中所說,您的回答不是有效的JSON。 與您發布的對象最接近的有效對象將格式化為:

var response = {
orders: [50],
0:{
    name: "#1347",
    line_items: [1],
    0:{
        fulfillment_service: "manual",
        fulfillment_status: null,
        gift_card: false,
        grams: 0,
        id: 966685828,
        price: "45.00",
        product_id: 455951420,
        quantity: 1,
        requires_shipping: true,
        sku: "",
        taxable: true,
        title: "Athletic Style Auto renew (ships every 1 Months)",
        variant_id: 1292559264,
        variant_title: "",
        vendor: null,
        name: "Athletic Style Auto renew (ships every 1 Months)",
        variant_inventory_management: null,
        properties: [9],
        0:  {
            name: "Glove Size",
            value: "M"
        },
        1:  {
            name: "Hat Size",
            value: "L/XL"
        },
        2:  {
            name: "Pant Size",
            value: "30x30"
        },
        3:  {
            name: "Right or Left Handed?",
            value: "Right"
        },
        4:  {
            name: "Shirt Size",
            value: "M"
        },
        5:  {
            name: "Shoe Size",
            value: "9"
        },
        6:  {
            name: "shipping_interval_frequency",
            value: "1"
        },
        7:  {
            name: "shipping_interval_unit_type",
            value: "Months"
        },
        8:  {
            name: "subscription_id",
            value: "1522"
        },
        product_exists: true,
        fulfillable_quantity: 1,
        total_discount: "0.00",
        tax_lines: [0]
      }
   }
};

這是有效的javascript對象,但不是JSON對象。

要成為有效的JSON,您需要將鍵作為字符串(“key”:value)

{
"orders": [50],
"0":{
    "name": "#1347",
    "line_items": [1],
    "0":{
        "fulfillment_service": "manual",
        "fulfillment_status": null,
        "gift_card": false,
        "grams": 0,
        "id": 966685828,
        "price": "45.00",
        "product_id": 455951420,
        "quantity": 1,
        "requires_shipping": true,
        "sku": "",
        "taxable": true,
        "title": "Athletic Style Auto renew (ships every 1 Months)",
        "variant_id": 1292559264,
        "variant_title": "",
        "vendor": null,
        "name": "Athletic Style Auto renew (ships every 1 Months)",
        "variant_inventory_management": null,
        "properties": [9],
        "0":  {
            "name": "Glove Size",
            "value": "M"
        },
        "1":  {
            "name": "Hat Size",
            "value": "L/XL"
        },
        "2":  {
            "name": "Pant Size",
            "value": "30x30"
        },
        "3":  {
            "name": "Right or Left Handed?",
            "value": "Right"
        },
        "4":  {
            "name": "Shirt Size",
            "value": "M"
        },
        "5":  {
            "name": "Shoe Size",
            "value": "9"
        },
        "6":  {
            "name": "shipping_interval_frequency",
            "value": "1"
        },
        "7":  {
            "name": "shipping_interval_unit_type",
            "value": "Months"
        },
        "8":  {
            "name": "subscription_id",
            "value": "1522"
        },
        "product_exists": true,
        "fulfillable_quantity": 1,
        "total_discount": "0.00",
        "tax_lines": [0]
      }
   }
}

如果要提取屬性,一旦有了對象的引用,就可以使用Javascript作為對象進行訪問

var myProperties = response["0"]["0"]["properties"];

在PHP上對響應執行JSON解析后,可以將它們作為嵌套關聯數組進行訪問。

 $myResponse = json_decode($response);
 $myProperties = $response['0']['0']['properties'];

數據格式不正確。 它缺少逗號和括號。 與下面的工作代碼比較,找到依賴項。

一旦數據格式正確,只需使用點和括號表示法即可訪問。 使用下面的代碼來更好地理解。

參考: Mozilla - 使用對象

  data['0']['0'].title;

運行代碼片段進行測試:

 <html> <body> Value: <div id="value" style="font-family:monospace;font-size:16px;"></div> <p> JSON: <textarea id="json" style="background-color:aliceblue;padding:0.5em;border:1px black solid;width:100%; height:40em;"></textarea> <script type="text/javascript"> var data = { orders: [50], 0: { name: "#1347", line_items: [1], 0: { fulfillment_service: "manual", fulfillment_status: null, gift_card: false, grams: 0, id: 966685828, price: "45.00", product_id: 455951420, quantity: 1, requires_shipping: true, sku: "", taxable: true, title: "Athletic Style Auto renew (ships every 1 Months)", variant_id: 1292559264, variant_title: "", vendor: null, name: "Athletic Style Auto renew (ships every 1 Months)", variant_inventory_management: null, properties: [9], 0: { name: "Glove Size", value: "M" }, 1: { name: "Hat Size", value: "L/XL" }, 2: { name: "Pant Size", value: "30x30" } } } }; document.getElementById('value').innerHTML = data['0']['0'].title; document.getElementById('json').value = 'json:\\n' + JSON.stringify(data, null, ' '); </script> </body> </html> 

如果你想使用PHP,聽起來你正在尋找json_decode

PHP

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));

?>

http://php.net/manual/en/function.json-decode.php

在您的方案中:

$response = //your api response
$php_obj = json_decode($response);

//Print a specific order and specific items properties
var_dump($php_obj->orders[0]->line_items[0]->properties);

//Print the properties for each item in each order 
//(prefixed with the index of the order number and item number)
foreach ($php_obj->orders as $order_index => $order) {
   foreach ($order->line_items as $item_index => $item) {
     echo "order: {$order_index} item: {$item_index}";
     var_dump($item->properties);
   }
}

如果你想使用JS

PHP / JS

<script>
  var response = "<?php echo $response; /*again your api response*/ ?>";
  var obj = JSON.parse(response);
  console.log(obj.orders[0].line_items[0].properties)
</script>

暫無
暫無

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

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