dos8410 2015-01-20 11:20
浏览 123

使用php-jsonpath在json中获取节点

I've a json structure :

{
    "Photos":{
        "Photo":[
            {
                "ID" : 111,
                 "type" : "JPEG",
                "URL": "blabla"  
            },
            {
                "ID": 222,
                "type": "JPG",
                "URL": "blaaaaaaaaa"
            }
        ]
    }
}

Am attempting to use php-jsonpath (By stefan Goessner : http://goessner.net/articles/JsonPath) to fetch node (photo) where ID == 222. And then, if found, insert a new data (array) into that node. Such that final output becomes :

{
    "Photos":{
        "Photo":[
            {
                "ID" : 111,
                 "type" : "JPEG",
                "URL": "blabla"  
            },
            {
                "ID": 222,
                "type": "JPG",
                "URL": "blaaaaaaaaa",
                "New_data" : {"CROP": "5x7", "Pixel": "none"}
            }
        ]
    }
}

currently i have this query string:

$parser = new Services_JSON(SERVICE_JSON_LOOSE_TYPE);
$jsonobj = $parser->decode(file_get_contents("filename.json", true));

$result = jsonPath($jsonobj , "$..Photo[?(@.ID == 222)]");   //is this expression wrong ?

i am only expectant of the node with the said ID (222) as returned $result, so that i can do this :

if($result != false){
     $result[] = array("New_data" => array("CROP" => 5x7, "Pixel" => "none"));
}

Unfortunately i go the error message :

Notice: Array to string conversion in C:\Server\wamp\www\MyCMS\jsonpath.php(119) 

What sin am i commiting ? and how do i get around it ? any code snippet (if necessary) will be appreciated. thanks !

  • 写回答

1条回答 默认 最新

  • dongze5043 2015-01-20 11:35
    关注

    You can use loop for iterating values-

           $list = array();
            foreach ($_POST['Photos']['Photo'] as $key => $value) {
                        $list[] = $value;
                }
    
    评论

报告相同问题?

悬赏问题

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