简体   繁体   English

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

[英]json_decode returns json string not an array

<?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":"20 November, 2016","EventType":"party"}

Json Data posted is: 杰森发布的数据是:

 {"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数据而不是关联数组。

It looks like @tkausl is correct. 看来@tkausl是正确的。 The JSON you're receiving has been double-encoded. 您收到的JSON已被双重编码。 Since it's double-encoded, a temporary solution would be to double-decode it. 由于它是经过双重编码的,因此临时解决方案是对其进行双重解码。

$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). 但是真正的解决方案是弄清楚为什么会这样开始并进行修复(如果要修复的话)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM