[英]2D json array erroneously gets converted to a string
我有一个内部有2个元素的外部数组。 2个元素中的每一个都是一个数组。 所以它是一个二维数组。
这是创建数组的方式:
$outerArray = array();
$nestedArray = array("first", "second", "third", "fourth");
$outerArray[] = $nestedArray;
$nestedArray_2 = array("first", "second", "third", "fourth");
$outerArray[] = $nestedArray_2;
然后,我将这个数组存储如下:
$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;
这是我文件中的HTML隐藏元素“ jsonArray”:
<input type="hidden" id="jsonArray" />
我在上面所做的回显向我展示了数组,如下所示:
[["first","second","third","fourth"],["first","second","third","fourth"]]
这一切都很好。 现在很明显,隐藏的输入字段中存储了有效的2D json数组。
然后,我对数组进行以下操作-将其存储在会话中,重定向到另一个文件,该文件提取后回显2D数组:
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);
因此,此操作会将json编码的二维数组发送到handler.php。 handler.php所做的所有工作都是将json编码的二维数组保存在$ _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
在alert()框中显示xmlhttp.responseText之后,我重定向到theViewer.php。 我从SESSION中检索2D数组并回显它:
// INSIDE theViewer.php
$jsonified_2D_Array = $_SESSION['jsonEncoded2D_Array'];
但是,当我回显刚刚从SESSION中检索到的“数组”时,它现在是字符串,而不是数组:
// 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;
上面回显的输出是这样的:
Okay, here is the 2D array:
first,second,third,fourth,first,second,third,fourth
我不明白 如果您看上面的代码,我将其放入隐藏的输入字段后回显此数组,它看起来像2D数组一样正确 。 那是在我将其转换为JSON之后。
然后,我从该隐藏字段中取出该JSON格式的2D数组,将其通过Ajax传递给handler.php以便保存在SESSION中,然后从ViewViewer.php中的SESSION中取出它 - 否更长的二维数组 -现在只是一个文本字符串。
我需要将此2D数组保留为2D数组而不是字符串。
这可能吗? 我需要在Viewer.php中使用2D数组(而不是字符串化数组) 。
我在Viewer.php中的字符串化数组上尝试了JSON.parse()和eval(),它停止了代码执行,但没有帮助。
$asd = '[["first","second","third","fourth"],["first","second","third","fourth"]]';
var_dump(json_decode($asd));
输出
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"
}
}
所以json_decode()是答案
编辑:所以答案被包装在这样的引号内
echo "document.getElementById('jsonArray').value = \'$jsonArray\';";
因此ajax会将其视为字符串,并且不会删除方括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.