简体   繁体   English

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

[英]Parse JSON from REST API output and put into respective variables

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 . 我正在使用php并从JIRA API获得了输出,如下所示,该输出使用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: 它可以很好地获取记录,但是我只想从所有记录中获取很少的信息,所以用print_r($issue['description'])编写在代码下面,但是会抛出错误:

Uncaught Error: Cannot use object of type chobie\\Jira\\Issue as array 未捕获的错误:无法将类型为chobie \\ Jira \\ Issue的对象用作数组

  1. What changes do I need to make in place of $issue to get these 3 information - [id:protected] , [description] and [name] . 我需要进行哪些更改来代替$ issue以获得这3个信息- [id:protected][description][name]

  2. How do I count total bugs found for eg number of chobie\\Jira\\Issue Object found? 如何计算发现的错误总数,例如找到的chobie \\ Jira \\ Issue对象的数量?

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]. 因此,$ issue会打印以下所有内容,但我只想获取[id:protected],[description]和[name]。 What changes i need to make in place of $issue to get these 3 information. 我需要进行哪些更改来代替$ issue以获得这3个信息。 Can someone please give me some hint? 有人可以给我一些提示吗?

JSON: 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
            )

        )
)

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

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

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