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

使用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 !

图片转代码服务由CSDN问答提供 功能建议

我有一个json结构:

  {
  “照片”:{
“照片”:[
 {
“ID”:111,
“类型”:“JPEG”,
“URL”:“blabla”
},
 {\  n“ID”:222,
“类型”:“JPG”,
“URL”:“blaaaaaaaa”
} 
] 
} 
} 
   
  
 

我试图使用php-jsonpath(作者:stefan Goessner: http://goessner.net/articles/ JsonPath )获取ID == 222的节点(照片)。然后,如果找到,则将新数据(数组)插入该节点。 这样最终输出变为:

  {
“Photos”:{
“Photo”:[
 {
“ID”:111,
“类型 “:”JPEG“,
”URL“:”blabla“
},
 {
”ID“:222,
”type“:”JPG“,
”URL“:”blaaaaaaaa“,  
“New_data”:{“CROP”:“5x7”,“Pixel”:“none”} 
} 
] 
} 
} 
   
 
 <  p>目前我有这个查询字符串: 
 
 
  $ parser = new Services_JSON(SERVICE_JSON_LOOSE_TYPE); 
 $ jsonobj = $ parser-&gt; decode(file_get_contents(“filename.json”  “,true)); 
 
 $ result = jsonPath($ jsonobj,”$ .. Photo [?(@。ID == 222)]“);  //这个表达式是错误的吗?
   
 
 

我只期望具有所述ID(222)的节点作为返回的$ result,所以 我可以这样做 :

  if($ result!= false){
 $ result [] = array(“New_data”=&gt; array(“CROP”=&gt; 5x7,“ 像素“=&gt;”无“)); 
} 
   
 
 

不幸的是我发错了信息:

 < 代码>注意:C:\ Server \ wamp \ www \ MyCMS \ jsonpath.php(119)
   
 
中的数组到字符串转换

我犯的是什么罪? 我该如何解决这个问题? 任何代码片段(如有必要)将不胜感激。 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;
                }
    
    打赏 评论

相关推荐 更多相似问题