简体   繁体   English

使用PHP从Redmine API解析json回答

[英]Parse json answer from Redmine API with PHP

I'm trying to parse a JSON answer from Redmine API and I don't know how to get to the parts of the array. 我正在尝试从Redmine API解析JSON答案,我不知道如何到达数组的各个部分。

Here is the code: 这是代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://login:password@redmine.server/redmine/issues.json?cf_2=12345');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);

$data = json_decode($response);

When I make a var_dump($data), the answer looks like this: 当我创建一个var_dump($ data)时,答案如下所示:

array(1) { [0]=> object(stdClass)#1853 (14) { ["id"]=> int(96) ["project"]=> object(stdClass)#1852 (2) { ["id"]=> int(68) ["name"]=> string(7) "Test.......

So, when I make a for loop, I would like to access the parts of the array: 所以,当我进行for循环时,我想访问数组的各个部分:

foreach($data as $issues){
    var_dump($issues["id"]);
}

And so on. 等等。 Any idea on this? 有什么想法吗?

Stupid me... 愚蠢的我...

The culprit was here: 罪魁祸首在这里:

$data = json_decode($response);

Should be: 应该:

$data = json_decode($response,true);

Now I get a proper PHP array. 现在我得到一个合适的PHP数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM