我正在使用Unirest库获得响应,我需要分离数据,以便基于该数据我可以调用我的下一个查询。这是我在使用Unirest库时获得的完整json响应
echo '<pre>'; print_r($response->raw_body); echo '</pre>';
{
"status": "success",
"images": [
"http://www.example.com/12.jpg"
],
"photos": [
{
"url": "http://www.example.com/12.jpg",
"width": 205,
"tags": [
{
"confidence": 0.978945010372561,
"center": {
"y": 64,
"x": 129
},
"height": 79,
"width": 79,
"tid": "31337",
"attributes": [
{
"smile_rating": 0.56,
"smiling": true,
"confidence": 0.56
}
],
"uids": [
{
"confidence": 0.35399999999999998,
"prediction": "SE2",
"uid": "SE2@SEA1"
},
{
"confidence": 0.28999999999999998,
"prediction": "SE1",
"uid": "SE1@SEA1"
},
{
"confidence": 0.16,
"prediction": "Star1",
"uid": "Star1@SEA1"
},
{
"confidence": 0.106,
"prediction": "SE3",
"uid": "SE3@SEA1"
},
{
"confidence": 0.037999999999999999,
"prediction": "SE6",
"uid": "SE6@SEA1"
},
{
"confidence": 0.035000000000000003,
"prediction": "SE5",
"uid": "SE5@SEA1"
},
{
"confidence": 0.017999999999999999,
"prediction": "SE4",
"uid": "SE4@SEA1"
}
]
}
],
"height": 206
}
]
}
我想要的是像这样打印
Confidence : 0.35399999999999998
Similar: Test2
答案 0 :(得分:0)
好吧,如果它是一个有效的JSON字符串,只需使用带有json_decode()
标志的简单true
,以便它返回一个数组。例如:
$data = json_decode($response->raw_body, true);
$i = 0;
foreach($data['photos'][0]['tags'][0]['uids'] as $value) {
if($i == 5) break;
echo 'Confidence: ' . $value['confidence'] . '<br/>';
echo 'Similar: ' . $value['prediction'] . '<br/><br/>';
$i++;
}
答案 1 :(得分:0)
Unirest 2.0的发布有很多改进,包括设置custom JSON decode flags
的功能这使您可以更好地控制响应体类型解析方法(json_decode)
免责声明:我是unirest-php的作者,我在Mashape工作。