json_decode返回json字符串而不是数组

时间:2022-10-26 13:44:56
<?php

$json=file_get_contents('php://input',true);
$data = json_decode($json, true);

print_r($data);
?>

Output given is {"EventTitle":"Game","EventBody":"body","EventDate":"20 November, 2016","EventType":"party"}

输出为{"EventTitle":"Game","EventBody":"body","EventDate":" 11月20日","EventType":"party"}

Json Data posted is:

Json数据发布:

 {"EventTitle":"Game","EventBody":"body","EventDate":"20 November, 2016","EventType":"party"}

Writing the json data in a variable and passing it to json_decode works but posting the same from the "php://input" returns a JSON data instead of associative array.

在变量中写入json数据并将其传递给json_decode可以工作,但在“php://input”中发布相同数据将返回json数据,而不是关联数组。

1 个解决方案

#1


2  

It looks like @tkausl is correct. The JSON you're receiving has been double-encoded. Since it's double-encoded, a temporary solution would be to double-decode it.

看起来@tkausl是正确的。您正在接收的JSON是双编码的。由于它是双编码的,一个临时的解决方案是对它进行双解码。

$data = json_decode(json_decode($json), true);

But the real solution is to figure out why it's like that to begin with and fix it (if it's yours to fix).

但真正的解决办法是弄清楚为什么一开始是这样的,然后修复它(如果是你的)。

#1


2  

It looks like @tkausl is correct. The JSON you're receiving has been double-encoded. Since it's double-encoded, a temporary solution would be to double-decode it.

看起来@tkausl是正确的。您正在接收的JSON是双编码的。由于它是双编码的,一个临时的解决方案是对它进行双解码。

$data = json_decode(json_decode($json), true);

But the real solution is to figure out why it's like that to begin with and fix it (if it's yours to fix).

但真正的解决办法是弄清楚为什么一开始是这样的,然后修复它(如果是你的)。