我从knockout.js生成了一个Json并创建了这样的东西:
{"po":"11446019431405","orders":[{"product":{"ID":"2","product_name":"A","product_code":""},"ID":"2","product_name":"A","product_code":"","price":"11","qty":1,"discount":0,"subtotal":11,"amount":11},{"product":{"ID":"1","product_name":"B","product_code":""},"ID":"1","product_name":"B","product_code":"","price":"10","qty":1,"discount":0,"subtotal":"10","amount":10}],"grandTotal":21}
我从Yii尝试了json_decode和Json :: decode,但它没有用。什么地方出了错?有没有一种特殊的方法可以在PHP中解析它?
以下是我的参考代码。
我的Javascript代码:
$.post(url, ko.toJSON(orderCollection), {}, "json");
在我的PHP代码中我尝试了$ _POST和Yii :: $ app-> request-> post()并且都失败了
public function actionPostorders() {
$request = Yii::$app->request->post();
echo Json::decode($request);
}
使用Json :: decode时出现错误:
PHP Warning 'yii\base\ErrorException' with message 'json_decode() expects parameter 1 to be string, array given'
in C:\xampp\yii2\controllers\PurchaseController.php:121
使用vanilla PHP时会出现类似的错误
json_decode($_POST)
PHP Warning 'yii\base\ErrorException' with message 'json_decode() expects parameter 1 to be string, array given'
in C:\xampp\yii2\controllers\PurchaseController.php:121
这是我在vardump我的$ _POST
时得到的array(1) { ["{"po":"11446021195934","orders":"]=> array(1) { ["{"product":{"ID":"2","product_name":"A","product_code":""},"ID":"2","product_name":"A","product_code":"","price":"11","qty":1,"discount":0,"subtotal":11,"amount":11},{"product":{"ID":"1","product_name":"B","product_code":""},"ID":"1","product_name":"B","product_code":"","price":"10","qty":1,"discount":0,"subtotal":"10","amount":10}"]=> string(0) "" } }
答案 0 :(得分:2)
您的问题是$_POST
是一个数组,您确实想要
json_decode($_POST[0]);
仅解码后期数组中的第一个对象。
然而,你很快就会注意到这也不会起作用; $_POST
数组要求您传入键值json对象或数组。在你的情况下,你确实传递了一个json对象,但它变得奇怪,因为你没有通过一个键。我并不完全确定这一点,但根据你的var_dump
ed数组的外观,我确定问题是你没有在数组中传递它。我还想指出,如果你设置了正确的标题并且只是传递了对象,那么后端应该能够读取它,就像它读取form-data
答案 1 :(得分:1)
根据JSLint
,您的JSON是正确的{
"po":"11446019431405",
"orders":
[
{
"product":
{
"ID":"2","product_name":"A",
"product_code":""
},
"ID":"2",
"product_name":"A",
"product_code":"",
"price":"11","qty":1,
"discount":0,
"subtotal":11,
"amount":11
},
{
"product":
{
"ID":"1","product_name":"B",
"product_code":""
},
"ID":"1",
"product_name":"B",
"product_code":"",
"price":"10",
"qty":1,
"discount":0,
"subtotal":"10",
"amount":10
}
],
"grandTotal":21
}
压缩:
{"po":"11446019431405","orders":[{"product":{"ID":"2","product_name":"A","product_code":""},"ID":"2","product_name":"A","product_code":"","price":"11","qty":1,"discount":0,"subtotal":11,"amount":11},{"product":{"ID":"1","product_name":"B","product_code":""},"ID":"1","product_name":"B","product_code":"","price":"10","qty":1,"discount":0,"subtotal":"10","amount":10}],"grandTotal":21}
用JSLint运行这个压缩的,没有出错。因此,处理JSON可能会出错。
答案 2 :(得分:0)
我不知道Json :: decode正在做什么,但是使用标准的php它应该返回一个对象,所以你不能回显对象或数组,尝试print_r或var_dump这样的变量。
$json = '{"po":"11446019431405","orders":[{"product":{"ID":"2","product_name":"A","product_code":""},"ID":"2","product_name":"A","product_code":"","price":"11","qty":1,"discount":0,"subtotal":11,"amount":11},{"product":{"ID":"1","product_name":"B","product_code":""},"ID":"1","product_name":"B","product_code":"","price":"10","qty":1,"discount":0,"subtotal":"10","amount":10}],"grandTotal":21}';
print_r( json_decode($json));
结果
stdClass Object
(
[po] => 11446019431405
[orders] => Array
(
[0] => stdClass Object
(
[product] => stdClass Object
(
[ID] => 2
[product_name] => A
[product_code] =>
)
[ID] => 2
[product_name] => A
[product_code] =>
[price] => 11
[qty] => 1
[discount] => 0
[subtotal] => 11
[amount] => 11
)
[1] => stdClass Object
(
[product] => stdClass Object
(
[ID] => 1
[product_name] => B
[product_code] =>
)
[ID] => 1
[product_name] => B
[product_code] =>
[price] => 10
[qty] => 1
[discount] => 0
[subtotal] => 10
[amount] => 10
)
)
[grandTotal] => 21
)
答案 3 :(得分:0)
你能试试这段代码吗?
$request = Yii::$app->request->post();
$jsondecode = CJSON::decode($request, true);
print_r($jsondecode);