dtpw54085 2015-08-13 11:26
浏览 127

php上的递归JSON解码

I have a data save on sql, and when I import it to php and try to decode into a object, I am finding a little problem because there are many objects inside the main object. The data I have is this:

[{
    "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&aacute;is?</p>"
}]

"Other" is and object inside each object of this array. When I make a json_decode for the array, I get the array without problems, but if I try to get the other object I get NULL. Finally I found a solution, but I am not sure if it is the best (and this is the reason of my question):

$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 is the original data from the data base.
The problem I think is happen is that in the ORIGINAL OTHER the text shown hasn´t the scape backslash for the double quotes -> \", instead what I see is:

{
    "father":"["father-3","undefined","father-class"]",
    "element":"["element-3","height: 265px;","element-class"]"
}

And for this reason I need to make all of this str_replace that I am not loving, and I think that must to be another way to do it.
I am watching that there is a $depth limit to decode function, I thought it was for decoding recursive objetcs, but I am not sure how it works.

Thank you.

  • 写回答

1条回答 默认 最新

  • doujiao4710 2015-08-13 13:44
    关注

    you should remove the double quote when opening and closing the braces and bracket of nested object/array like this

    "other":{
        \"father\":[\"father-3\",\"undefined\",\"father-class\"],
        \"element\":[\"element-3\",\"height: 265px;\",\"element-class\"]
    },
    

    and it should decode fine, hope it helps

    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂