doudao1950 2014-04-10 07:20
浏览 58

php解码json数组

I have a php page that receives a json object from javascript page, but i was not able to decode the json in php. How to decode the json and store in php such that $arr[0]=[1,2,34,5,2]; $arr[1]=[2,1,34,5,2]; $arr[2]=[8,1,34,5,2]; in php ?

after removing "myString = JSON.stringify(myObject);"
echo $value; outputs "Array"
echo $value[0]; outputs nothing
echo $value->{"key"};  outputs nothing either

how can i actually get the array contents?

javascript:

var mon=[1,2,34,5,2];
var tue=[2,1,34,5,2];
var wed=[8,1,34,5,2];
var myObject = {'key' :'value','key2':'value','key3':'value'};

myObject.key = mon;
myObject.key2 = tue;
myObject.key3 = wed;

 myString = JSON.stringify(myObject); //this line removed

var jsonString = JSON.stringify(myObject);

$.ajax({
    type: "POST",
    url: "n3.php",
    data: {data : jsonString}, 
    cache: false,

    success: function(aaa){ 
        alert("OK");

         $("#pageContent").html(aaa);       
    }
});

php:

<?php
$value = json_decode($_POST['data']);
echo $value;     //this echos the whole json object 
echo $value->{"key"};  //this outputs nothing
?>
  • 写回答

1条回答 默认 最新

  • doudi2431 2014-04-10 07:24
    关注

    You are JSON encoding your data twice on the Javascript side. When you call json_encode in PHP once, you get a JSON encoded object back. That's why echo $value outputs the whole string. If it was a PHP array at this point it would output "Array" or an error in case it was an object, it would not output the whole content.

    Either json_decode it again, or don't double encode it in Javascript.

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制