循环JSON数据仅显示第一个字符

时间:2019-01-16 01:22:54

标签: php json foreach jira

我正在尝试输出来自JIRA的特定信息,我有JSON循环并显示了结果,但是嵌套最远的结果仅显示了第一个字符。

我尝试遵循建议here,但这似乎是为了获得顶级结果,我似乎无法使其与我的代码一起使用。

<?php
$username = '*************';
$password = '*************';

$url = '<JIRA-DOMAIN>/rest/api/2/search?jql=project=ICI&fields=key,summary,description&maxResults=10';

$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

$issue_list = (curl_exec($curl));
$json = json_decode($issue_list, TRUE);
?>

<div class="toto">
    <?php
    foreach($json['issues'] as $obj){   ?>

        <h2><?php echo $obj['id']; ?></h2>

          <?php foreach($obj['fields'] as $parc){ ?>

             <a href="<?php echo $parc['summary']; ?>">
                 <span><?php echo $parc['description']; ?></span>
             </a>

       <?php }
     } ?>
</div>

当我回显$ issue_list时,我得到以下信息:

输出缩短

{
    "expand":"schema,names",
    "startAt":0,
    "maxResults":10,
    "total":9,
    "issues":[
        {

"expand":"operations,versionedRepresentations,editmeta,changelog,renderedFields",
        "id":"10058",
        "self":"<domain>/rest/api/2/issue/10058",
        "key":"ICI-9",
        "fields":{
            "summary":"ToS and PP button removal",
            "description":"The Terms of Service and Privacy Policy buttons go to a page on the website so they can't use the app after. They also go to a page of nothing because they aren't written so they need to be deleted"
        }
    },
    {
        "expand":"operations,versionedRepresentations,editmeta,changelog,renderedFields",
        "id":"10048",
        "self":"<domain>/rest/api/2/issue/10048",
        "key":"ICI-1",
        "fields":{
            "summary":"Create Launch Image",
            "description":"Create launch image for app. Resolution should be: 2048px × 1536px\r\n\r\n[~james.whitaker] I got this info from [here|https://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/launch-screen/]"
        }
    }
]
}

但是循环输出仅显示描述和摘要的第一个字符,如下所示,但显示整个ID。

Output Screenshot

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

您的问题出在以下foreach循环中:

     <?php foreach($obj['fields'] as $parc){ ?>

         <a href="<?php echo $parc['summary']; ?>">
             <span><?php echo $parc['description']; ?></span>
         </a>

   <?php }

$parc的值将为$obj['fields']['summary'],然后在循环中为$obj['fields']['description'],因此当您尝试使用['summary']对其进行索引时,将使用无效的索引,即转换为0(因此为单个字符)。请尝试以下操作(删除foreach):

     <a href="<?php echo $obj['fields']['summary']; ?>">
         <span><?php echo $obj['fields']['description']; ?></span>
     </a>

答案 1 :(得分:0)

我认为fields不是数组。

$parc['summary']更改为$obj['fields']['summary']