好吧,一些基本信息...
PHP 5.3.5
JSON 1.2.1
这一切都在本地 XAMPP 服务器上运行。基本上,我下面的代码返回一个不完整的关联数组。json_last_error() 返回 0。我已经搜索了网络,但在没有返回特定 json 错误的情况下找不到此类错误。
<?php
$ShippingObj = json_decode($_POST['formData'], true);
echo json_last_error();
echo $ShippingObj;
我已经用 JSONLint 验证了这个字符串。这就是它的输出......
array (
'name' => 'newShipment',
'comments' => '',
'count' => 1,
'itemID' => 0,
'itemWeight' => 7.548,
'itemLength' => 0,
'itemWidth' => 0,
'itemHeight' => 0,
'limits' => '',
'collection' =>
array (
0 =>
array (
'name' => '8x6x4 Standard',
'comments' => '',
'count' => 5,
'itemID' => '3',
'itemWeight' => 7.5475,
'itemLength' => '8',
'itemWidth' => '6',
'itemHeight' => '4',
'limits' => 'MFR',
'collection' =>
array (
0 =>
array (
'ID' => '1',
'Name' => ' TA-200 Demarcation Enclosure',
'Qty' => 1,
'Weight' => '0.34',
),
1 =>
array (
'ID' => '2',
'Name' => ' CG-1000 Demarcation Enclosure',
'Qty' => 4,
'Weight' => '1',
),
2 =>
array (
'ID' => '19',
'Name' => ' FM-02 Stereo FM Transmitter',
'Qty' => 1,
'Weight' => '0.1625',
),
3 =>
array (
'ID' => '20',
'Name' => ' Renard 64 Rev XC Parts Kit w/PCB',
'Qty' => 1,
'Weight' => '1.125',
),
4 =>
array (
'ID' => '21',
'Name' => ' SSRez Parts Kit w/PCB - Set of 8',
'Qty' => 2,
'Weight' => '0.86',
),
),
),
),
)
这是一个有效的 JSON 构造.....这是我复制到http://www.functions-online.com/json_decode.html以验证我的字符串的输入。
$returnValue = json_decode('{"name":"newShipment","comments":"","count":1,"itemID":0,"itemWeight":7.548,"itemLength":0,"itemWidth":0,"itemHeight":0,"limits":"","collection":{"0":{"name":"8x6x4 Standard","comments":"","count":5,"itemID":"3","itemWeight":7.5475,"itemLength":"8","itemWidth":"6","itemHeight":"4","limits":"MFR","collection":{"0":{"ID":"1","Name":" TA-200 Demarcation Enclosure","Qty":1,"Weight":"0.34"},"1":{"ID":"2","Name":" CG-1000 Demarcation Enclosure","Qty":4,"Weight":"1"},"2":{"ID":"19","Name":" FM-02 Stereo FM Transmitter","Qty":1,"Weight":"0.1625"},"3":{"ID":"20","Name":" Renard 64 Rev XC Parts Kit w/PCB","Qty":1,"Weight":"1.125"},"4":{"ID":"21","Name":" SSRez Parts Kit w/PCB - Set of 8","Qty":2,"Weight":"0.86"}}}}}', true);
第一部分代码返回 $ShippingObj 的以下内容。注意没有展开的“[collection] => array(5)”......它是空的。我检查了以前 JS 中的字符串,没问题。
array(10) (
[name] => (string) newShipment
[comments] => (string)
[count] => (int) 1
[itemID] => (int) 0
[itemWeight] => (float) 16.2955
[itemLength] => (int) 0
[itemWidth] => (int) 0
[itemHeight] => (int) 0
[limits] => (string)
[collection] => array(1) (
[0] => array(10) (
[name] => (string) CG2000 OEM Box
[comments] => (string)
[count] => (int) 5
[itemID] => (string) 2
[itemWeight] => (float) 8.1475
[itemLength] => (string) 21
[itemWidth] => (string) 16.5
[itemHeight] => (string) 12.5
[limits] => (string) 2ND
[collection] => array(5) (
)
)
)
)