我在sql上保存了一个数据,当我将它导入php并试图解码到一个对象时,我发现了一个小问题,因为主对象中有很多对象。我掌握的数据如下:
[{
"other":"{
\"father\":\"[\"father-3\",\"undefined\",\"father-class\"]\",
\"element\":\"[\"element-3\",\"height: 265px;\",\"element-class\"]\"
}",
"type":"login",
"columns":4,
"offsets":0,
"order":"father,facebook,twitter,email",
"father":"{login-title}"
},
{
"other":"{
\"text\":\"[\"text-4\",\"\",\"text-class\"]\",
\"element\":\"[\"element-4\",\"height: 265px;\",\"element-class\"]\"
}",
"type":"text",
"columns":4,
"offsets":2,
"order":"text",
"text":"<p>hola <strong>muchachos </strong>como estáis?</p>"
}]"Other“是和对象在这个数组的每个对象中。当我为数组创建一个json_decode时,我可以毫无问题地得到数组,但是如果我试图获得另一个对象,我就会得到NULL。最后,我找到了一个解决办法,但我不确定这是否是最好的(这就是我提出问题的原因):
$elements = json_decode($value);
foreach($elements as $element)
{
echo "<h1>ELEMENTO</h1>";
var_dump($element);
echo "<h1>ORIGINAL OTHER</h1>";
echo "<textarea>".$element->other."</textarea>";
$element->other = str_replace("\"]", "\\\"]",$element->other);
$element->other = str_replace("[\"", "[\\\"",$element->other);
$element->other = str_replace("\",\"", "\\\",\\\"",$element->other);
$element->other = str_replace("]\\\",\\\"", "]\",\"",$element->other);
$other = json_decode($element->other);
echo "<h1>OTHER</h1>";
echo "<textarea>".$element->other."</textarea>";
var_dump($other);
}$value是数据库中的原始数据。
我认为发生的问题是,在原来的另一个文本中,显示的文本没有为双引号-> \“设置反斜杠,相反,我看到的是:
{
"father":"["father-3","undefined","father-class"]",
"element":"["element-3","height: 265px;","element-class"]"
}由于这个原因,我需要使所有的str_replace,我不爱,我认为这一定是另一种方式来做它。
我正在观察解码函数有一个$depth限制,我以为它是用于解码递归的objetcs,但我不知道它是如何工作的。
谢谢。
发布于 2015-08-13 13:44:48
在打开和关闭嵌套对象/数组的大括号和括号时,应删除双引号,如下所示
"other":{
\"father\":[\"father-3\",\"undefined\",\"father-class\"],
\"element\":[\"element-3\",\"height: 265px;\",\"element-class\"]
},它应该能很好的解码,希望它能帮上忙。
https://stackoverflow.com/questions/31986884
复制相似问题