doufan3958 2014-03-13 06:58
浏览 47
已采纳

2D json数组错误地转换为字符串

I have an outer array with 2 elements inside. Each of the 2 elements is an array. So it's a 2-dimensional array.

Here is how the array is created:

 $outerArray = array();
 $nestedArray =  array("first", "second", "third", "fourth");
 $outerArray[] = $nestedArray;
 $nestedArray_2 =  array("first", "second", "third", "fourth");
 $outerArray[] = $nestedArray_2;

I then store this array as follows:

 $jsonArray = json_encode($outerArray);

 // USE 'echo' TO PUT THIS array INTO A HIDDEN input FIELD ON THE PAGE
 echo "document.getElementById('jsonArray').value = $jsonArray;";  


 // LETS ECHO THE NESTED ARRAY TO VERIFY IT WAS CONSTRUCTED CORRECTLY
 echo $jsonArray;

Here is the HTML hidden element 'jsonArray' in my file:

  <input type="hidden" id="jsonArray" />

That echo I did just above here shows me the array looks like this:

  [["first","second","third","fourth"],["first","second","third","fourth"]]

All this is good. Right now it's clear that the hidden input field has a valid 2D json array stored in it.

I then do the following with the array -- I store it in the session, redirect to another file, which extracts then echoes the 2D array:

   var xmlhttp=new XMLHttpRequest();
   xmlhttp.onreadystatechange=function()
   { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
          // SEE NOTE #1 BELOW ABOUT WHAT THIS SHOWS IN THE ALERT() BOX
          alert("The xmlhttp.responseText is: " + xmlhttp.responseText);


          window.location = "theViewer.php";
      }
   }

   var arg = "theStored2dimensionArrayAsJson=" 
                   + document.getElementById('jsonArray').value;
   xmlhttp.open("POST", "handler.php", true);
   xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   xmlhttp.send(arg);

So what this does, it sends the json-encoded 2-dimensional array to handler.php. And all handler.php does is save the json-encoded 2-dimension array in the $_SESSION:

   // in handler.php
   $_SESSION['jsonEncoded2D_Array'] = $_POST['theStored2dimensionArrayAsJson'];

   // echo out the array so it appears in the xmlhttp.responseText
   echo $_POST['theStored2dimensionArrayAsJson'];


   // NOTE #1 -- THIS IS WHAT IS DISPLAYED IN THE xmlhttp.responseText
   handler.php, the jsonEncoded2D_Array is:          
        first,second,third,fourth,first,second,third,fourth

After displaying the xmlhttp.responseText in an alert() box, I redirect to theViewer.php. I retrieve the 2D array from the SESSION and echo it out:

   // INSIDE theViewer.php
   $jsonified_2D_Array = $_SESSION['jsonEncoded2D_Array']; 

But when I echo out the 'array' I just retrieved from the SESSION, it's a string now, not an array:

   // THE echo SHOWS THE 2D ARRAY HAS BEEN COVERTED TO A TEXT STRING
   // AND IS NO LONGER AN ARRAY
   echo "Okay, here is the 2D array:<br />" . $jsonified_2D_Array;

The output of the echo just above is this:

    Okay, here is the 2D array:
    first,second,third,fourth,first,second,third,fourth

I don't get this. If you look at the code above, I echo this array after putting it into a hidden input field and it looks correct, like a 2D array. And that is after I converted it to JSON.

Then I take that JSON-ified 2D array out of that hidden field, pass it via Ajax to handler.php in order to save in the SESSION, AND then take it back out of the SESSION in theViewer.php -- and it is no longer a 2d array -- it is now just a text string.

I need to keep this 2D array as a 2D array and not a string.

Is this possible? I need the 2D array (not a stringified array) in theViewer.php.

I tried JSON.parse() and eval() on the stringified array in theViewer.php and it stopped code execution, it did not help.

  • 写回答

1条回答 默认 最新

  • dongqiyang3711 2014-03-13 07:07
    关注
    $asd = '[["first","second","third","fourth"],["first","second","third","fourth"]]';
    var_dump(json_decode($asd));
    

    outputs

    array(2) {
      [0]=> array(4) {
            [0]=>
            string(5) "first"
            [1]=>
            string(6) "second"
            [2]=>
            string(5) "third"
            [3]=>
            string(6) "fourth"
      }
      [1]=> array(4) {
            [0]=>
            string(5) "first"
            [1]=>
            string(6) "second"
            [2]=>
            string(5) "third"
            [3]=>
            string(6) "fourth"
      }
    }
    

    so json_decode() is the answer

    EDIT: so the answer was wrapping it inside quotes like this

    echo "document.getElementById('jsonArray').value = \'$jsonArray\';"; 
    

    so ajax will treat it as string and will not remove brackets.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记