我有一个在Shopify中工作的API,当我使用特定任务的URL时,它会在JSON中退回响应。浏览器将其翻译成RAW。但是,我的问题是如何从JSON响应中获取特定值?我只想使用JavaScript或PHP脚本来提取"属性"部分。下面的代码不是文件。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,您需要键才能成为字符串("键":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"];
在您对响应进行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>