dongmeiba6151
2016-10-29 09:43 浏览 64

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

I'm using php and got output from JIRA API like below which connects to tool using https://github.com/chobie/jira-api-restclient/blob/master/README.md. It fetches records very fine but i just want few information from all records so written below code with print_r($issue['description']) but it throws error:

Uncaught Error: Cannot use object of type chobie\Jira\Issue as array

  1. What changes do I need to make in place of $issue to get these 3 information - [id:protected], [description] and [name].

  2. How do I count total bugs found for e.g. number of chobie\Jira\Issue Object found?

Code:

$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>";
}

So $issue prints everything like below but i just want to get [id:protected], [description] and [name]. What changes i need to make in place of $issue to get these 3 information. Can someone please give me some hint?

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条回答 默认 最新

  • 已采纳
    dongzhi9574 dongzhi9574 2016-10-29 21:34

    Got a answer, you need to convert object to array first, get array fields and parse it accordingly.

    $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>";
    }
    
    点赞 评论 复制链接分享

相关推荐