2017-02-02 44 views
0

下面的代碼不會將line_items呈現爲JSON數組,並且我已經搜索/詢問到無濟於事。連接到API時出現PHP/JSON數組問題

任何人都可以在這裏給我一隻手嗎?

我還包括$ orderShippingInfo變量的json_encode數據。

下面是JSON輸出:

{ 
"token":"API_KEY_HERE", 
"email":"[email protected]", 
"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' => '[email protected]', 
    '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

'$ orderShippingInfo = json_decode($ json的,真正的);' – Mohammad

+0

@Mohammad他是編碼PHP數組 – RiggsFolly

+0

讓您隨心所欲的line_items保持一個數組,而不是一個JSON字符串? – Pitt

回答

2

您需要將您的行項目數組作爲它們的數組。

例如:

$orderShippingInfo = array(
'token' => 'API_KEY_HERE', 
'email' => '[email protected]', 
'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', 
); 

然後就可以一個第二行項目如果需要添加到陣列。

+0

您的'shipping_lines'也可能是這樣的 – Andy

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' => '[email protected]', 
'line_items' => array(
    'sku'=>'12345', 
    'name'=>'Product Name', 
    'title'=>'Product Title', 
    'price'=> '$1.99', 
. . . 
. . . 
+0

這是正確的。 JavaScript沒有關聯數組,因此它必須將數據轉換爲對象。 – databyss

+0

感謝您的澄清,我忘記了原因。 – RiggsFolly

相關問題