dongmeiba6151 2016-10-29 09:43
浏览 69
已采纳

从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 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>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误