我的JSON数组:
[\"{\\\"mapurl\\\":\\\"http:\\\\\\/\\\\\\/maps.google.com\\\\\\/maps?q=17.xxxxx5,78.xxxxxx3\\\",\\\"caller\\\":\\\"+91xxxxxx\\\",\\\"id\\\":1,\\\"reciever\\\":\\\"+91xxxxxx\\\",\\\"timpestamp\\\":\\\"3\\\"}\",\"{\\\"mapurl\\\":\\\"http:\\\\\\/\\\\\\/maps.google.com\\\\\\/maps?q=17.xxxxx,78.xxxxx\\\",\\\"caller\\\":\\\"+91xxxxxx\\\",\\\"id\\\":2,\\\"reciever\\\":\\\"+91xxxxx\\\",\\\"timpestamp\\\":\\\"3\\\"}\"]
我在添加参数时将此JSONArray作为POST请求的一部分传递。
我显示数组内容的代码
$jsonData = stripslashes($_POST['add']);
$phpArray = json_decode($jsonData,true);
foreach ($phpArray as $index => $record)
{
echo $record["caller"];
}
我的输出只是两个开放的大括号。
{{
我无法理解什么是错的
答案 0 :(得分:0)
您的JSON无效,每个数组周围不应有引号。
修正生成它的内容,如果你不能解决下面的问题
$jsonData = stripslashes(stripslashes($jsonData));
$jsonData = str_replace(
array('"{', '}"'),
array('{', '}'),
$jsonData
);
$phpArray = json_decode($jsonData,true);
foreach ($phpArray as $index => $record)
{
echo $record["caller"];
}
虽然作为最后的手段使用,但在发送之前修复损坏的JSON是最佳选择。魔术引号也从PHP中删除。避免使用它
以上输出
+ 91xxxx + 91xxxxx
答案 1 :(得分:0)
你的json字符串有问题。 试试这个:
$post = '[{\\\"mapurl\\\":\\\"http:\\\\\\/\\\\\\/maps.google.com\\\\\\/maps?q=17.xxxxx5,78.xxxxxx3\\\",\\\"caller\\\":\\\"+91xxxxxx\\\",\\\"id\\\":1,\\\"reciever\\\":\\\"+91xxxxxx\\\",\\\"timpestamp\\\":\\\"3\\\"},{\\\"mapurl\\\":\\\"http:\\\\\\/\\\\\\/maps.google.com\\\\\\/maps?q=17.xxxxx,78.xxxxx\\\",\\\"caller\\\":\\\"+91xxxxxx\\\",\\\"id\\\":2,\\\"reciever\\\":\\\"+91xxxxx\\\",\\\"timpestamp\\\":\\\"3\\\"}]';
$jsonData = stripslashes(stripslashes($post));
$phpArray = json_decode($jsonData,true);
foreach ($phpArray as $index => $record)
{
echo $record["caller"];
}