下面的代码没有将line_items呈现为JSON数组,我搜索/询问周围没有用。
有人可以帮我一把吗?
我还包含了$ orderShippingInfo变量的json_encode数据。
以下是JSON输出:
{
"token":"API_KEY_HERE",
"email":"a.pinochet@chilehelicoptertours.com",
"line_items":{
"sku":"12345",
"name":"Product Name",
"title":"Product Title",
"price":"$1.99",
"quantity":"1",
"total_tax":"$0.00"
},
"shipping_lines":{
"title":"UPS",
"price":"$0.00",
"method":"UPS 3 DAY",
"carrier":"UPS"
},
"order_id":"0001",
"profile":"default",
"shipping_address":{
"province":"AZ",
"city":"Testville",
"first_name":"Augusto",
"last_name":"Pinochet",
"zip":"12341",
"province_code":"NY",
"country":"US",
"company":"Company Name, Inc.",
"phone":"1112223333",
"country_code":"US",
"address1":"123 Testing Dr Street",
"address2":"Suite #1"
},
"subtotal_price":"$0.00",
"created_at":"2017-02-02",
"country_code":"US",
"total_discounts":"$0.00",
"total_price":"$0.00"
}
TIA!
<?php
$orderShippingInfo = array(
'token' => 'API_KEY_HERE',
'email' => 'a.pinochet@chilehelicoptertours.com',
'line_items' => array(
'sku'=>'12345',
'name'=>'Product Name',
'title'=>'Product Title',
'price'=> '$1.99',
'quantity' => '1',
'total_tax' => '$0.00',
),
'shipping_lines' => array(
'title' => 'UPS',
'price' => '$0.00',
'method' => 'UPS 3 DAY',
'carrier' => 'UPS',
),
'order_id' => '0001',
'profile' => 'default',
'shipping_address' => array(
'province' => 'AZ',
'city' => 'Testville',
'first_name' => 'Augusto',
'last_name' => 'Pinochet',
'zip' => '12341',
'province_code' => 'NY',
'country' => 'US',
'company' => 'Company Name, Inc.',
'phone' => '1112223333',
'country_code' => 'US',
'address1' => '123 Testing Dr Street',
'address2' => 'Suite #1',
),
'subtotal_price' => '$0.00',
'created_at' => date('Y-m-d'),
'country_code' => 'US',
'total_discounts' => '$0.00',
'total_price' => '$0.00',
);
echo json_encode($orderShippingInfo);
?>
答案 0 :(得分:2)
你需要让你的行项目数组成为它们的数组。
例如:
$orderShippingInfo = array(
'token' => 'API_KEY_HERE',
'email' => 'a.pinochet@chilehelicoptertours.com',
'line_items' => array(
array(
'sku'=>'12345',
'name'=>'Product Name',
'title'=>'Product Title',
'price'=> '$1.99',
'quantity' => '1',
'total_tax' => '$0.00',
)
),
'shipping_lines' => array(
'title' => 'UPS',
'price' => '$0.00',
'method' => 'UPS 3 DAY',
'carrier' => 'UPS',
),
'order_id' => '0001',
'profile' => 'default',
'shipping_address' => array(
'province' => 'AZ',
'city' => 'Testville',
'first_name' => 'Augusto',
'last_name' => 'Pinochet',
'zip' => '12341',
'province_code' => 'NY',
'country' => 'US',
'company' => 'Company Name, Inc.',
'phone' => '1112223333',
'country_code' => 'US',
'address1' => '123 Testing Dr Street',
'address2' => 'Suite #1',
),
'subtotal_price' => '$0.00',
'created_at' => date('Y-m-d'),
'country_code' => 'US',
'total_discounts' => '$0.00',
'total_price' => '$0.00',
);
然后,如果需要,您可以向数组添加第二个订单项。
答案 1 :(得分:1)
原因是javascript没有关联数组概念,因此从json_encode()
生成一个会破坏javascript。
请参阅json_encode将执行的示例
$xx = ['a','b'];
$yy = ['one'=> 1, 'two'=>2];
print_r($xx);
echo json_encode($xx).PHP_EOL;
print_r($yy);
echo json_encode($yy);
Array
(
[0] => a
[1] => b
)
["a","b"] // note this is a JSON array
Array
(
[one] => 1
[two] => 2
)
{"one":1,"two":2} // note this is a JSON object
如果PHP数组以数字方式编制索引,则会获得JSON数组
如果PHP数组是assoc数组,它将创建一个JSON对象
如果由于某种原因你特别想要一个JSON字符串表示为一个数组,你可以像这样[]
添加$orderShippingInfo[] = array(
$orderShippingInfo[] = array(
'token' => 'API_KEY_HERE',
'email' => 'a.pinochet@chilehelicoptertours.com',
'line_items' => array(
'sku'=>'12345',
'name'=>'Product Name',
'title'=>'Product Title',
'price'=> '$1.99',
. . .
. . .