我有一个json结构:
{
"Photos":{
"Photo":[
{
"ID" : 111,
"type" : "JPEG",
"URL": "blabla"
},
{
"ID": 222,
"type": "JPG",
"URL": "blaaaaaaaaa"
}
]
}
}
我试图使用 php-jsonpath(作者:Stefan Goessner : http://goessner.net/articles/JsonPath) 来获取节点(照片),其中 ID == 222。然后,如果找到,则将新数据(数组)插入该节点。这样最终输出变为:
{
"Photos":{
"Photo":[
{
"ID" : 111,
"type" : "JPEG",
"URL": "blabla"
},
{
"ID": 222,
"type": "JPG",
"URL": "blaaaaaaaaa",
"New_data" : {"CROP": "5x7", "Pixel": "none"}
}
]
}
}
目前我有这个查询字符串:
$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 ?
我只期待返回$result具有所述 ID (222) 的节点,因此我可以这样做:
if($result != false){
$result[] = array("New_data" => array("CROP" => 5x7, "Pixel" => "none"));
}
不幸的是,我去了错误消息:
Notice: Array to string conversion in C:'Server'wamp'www'MyCMS'jsonpath.php(119)
我犯了什么罪?我该如何解决它?任何代码片段(如有必要)都将不胜感激。谢谢!
您可以使用循环来迭代值-
$list = array();
foreach ($_POST['Photos']['Photo'] as $key => $value) {
$list[] = $value;
}