weixin_33749131 2017-07-24 08:29 采纳率: 0%
浏览 24

JSON解码错误

I am sending php array as json format, but I was unable to decode the value. Here is how I made the array

    $ads = $atts['ads'];
    if (sizeof($ads) > 0) {
    foreach($ads as $social_item) {
        $sdbr = $social_item['sidebar'];
        $pno = $social_item['no'];
        $out[$sdbr] = $pno;
    }
}

Which output

array (
  'Full width ad 1' => 2,
  'sidebar-1' => 3,
)

Now I have json encode it

$myJSON = json_encode($out);

The json formatted value {"Full width ad 1":2,"sidebar-1":3} Then I am passing it through data attribute value

echo "<div data-ad = '$myJSON' class='ash_loadmore'><span>LOAD MORE</span>
</div>";

The out that I have got

$ad = $_POST['ad'];
array (
  'Full width ad 1' => '2',
  'sidebar-1' => '3',
)

So now time for decode the output

    $out = json_decode($ad,TRUE);
    var_dump($out); // Returns NULL although the array value is present

But if I put the json format data it works fine

    $out = json_decode('{"Full width ad 1":2,"sidebar-1":3}',TRUE);
    var_dump($out);

I suspect before json encode the array array(2) { ["Full width ad 1"]=> int(2) ["sidebar-1"]=> int(3) } value is int but I am getting value as string array(2) { ["Full width ad 1"]=> string(1) "2" ["sidebar-1"]=> string(1) "3" }

What's wrong doing by me?

  • 写回答

2条回答 默认 最新

  • weixin_33725722 2017-07-24 08:33
    关注

    After this step:

    $ad = $_POST['ad'];
    array (
      'Full width ad 1' => '2',
      'sidebar-1' => '3',
    )
    

    you see in the output that this is already a php array, so any json-decode method will fail (this is no json).

    You can work with the array right away, depending what you want to do ;)

    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R