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

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装