从REST API输出中解析JSON并将其放入相应的变量中

时间:2016-10-29 09:43:52

标签: php json

我使用php并从下面的JIRA API获得输出,该输出使用 https://github.com/chobie/jira-api-restclient/blob/master/README.md 连接到工具。它取得的记录非常精细,但我只想要所有记录中的信息很少,因此写在print_r($issue['description'])的代码下面,但它会引发错误:

  

未捕获错误:无法使用chobie \ Jira \ Issue类型的对象作为数组

  1. 我需要做些哪些更改来代替$ issue来获取这3条信息 - [id:protected][description][name]

  2. 如何计算找到的错误总数找到了chobie \ Jira \ Issue Object的数量?

  3. 代码:

    $walker = new Walker($api);
    $walker->push(
        'project = "SEA" AND (status != "closed" AND status != "resolved") ORDER BY priority DESC'
    );
    
    foreach ( $walker as $issue ) {
        print "<pre>";
        print_r($issue);
        print "</pre>";
    }
    

    所以$ issue打印下面的所有内容,但我只想得到[id:protected],[description]和[name]。为了获得这3个信息,我需要做出哪些改变来代替$ issue。有人可以给我一些提示吗?

    JSON:

    chobie\Jira\Issue Object
    (
        [id:protected] => 21373505
        [self:protected] => https://test.corp.com/rest/api/2/issue/21373505
        [key:protected] => S12E-7337
        [fields:protected] => Array
        (
            [Status] => Array
            (
                [self] => https://test.corp.com/rest/api/2/status/3
                [description] => Working on the issue
                [iconUrl] => https://test.corp.com/images/icons/statuses/assigned.png
                [name] => In Progress
                [id] => 3
                [statusCategory] => Array
                (
                    [self] => https://test.corp.com/rest/api/2/statuscategory/4
                    [id] => 4
                    [key] => indeterminate
                    [colorName] => yellow
                    [name] => In Progress
                )
    
            )
    )
    
    chobie\Jira\Issue Object
    (
        [id:protected] => 74534233
        [self:protected] => https://test.corp.com/rest/api/2/issue/74534233
        [key:protected] => ASE-7327
        [fields:protected] => Array
        (
            [Status] => Array
            (
                [self] => https://test.corp.com/rest/api/2/status/3
                [description] => This issue is being actively worked on at the moment by the assignee.
                [iconUrl] => https://test.corp.com/images/icons/statuses/assigned.png
                [name] => In Progress
                [id] => 3
                [statusCategory] => Array
                (
                    [self] => https://test.corp.com/rest/api/2/statuscategory/4
                    [id] => 6
                    [key] => indeterminate
                    [colorName] => yellow
                    [name] => In Progress
                )
    
            )
    )
    

1 个答案:

答案 0 :(得分:0)

得到答案,你需要先将对象转换为数组,获取数组字段并相应地解析它。

$walker = new Walker($api);
$walker->push('project = "SEA" AND (status != "closed" AND status != "resolved") ORDER BY priority DESC');

foreach ( $walker as $issue ) {
    $issue = $bugs->getFields();
    print "<pre>";
    print_r($issue);
    print_r(trim($issue['Status']['name']);
    print "</pre>";
}