简体   繁体   English

如何使用 PHP 和 json_decode 在循环中解码 JSON?

[英]How to decode JSON in a loop using PHP and json_decode?

I have json like this, returned from service:我有这样的 json,从服务返回:

{
    "reports" : {
        "name" : "reports"
        "response" : {
            "build" : {
                "version" : "2.55.10.0",
                "name" : "reports-app"
            }
        },
        "status" : 200
    },
    "static-application" : {
        "name" : "static-application"
        "response" : {
            "app" : {
                "name" : "client-frontend",
                "description" : "Client Static"
            },
            "build" : {
                "version" : "2.55.10.0",
                "name" : "client-frontend"
            }
        },
        "status" : 200
    },
    "static-help" : {
        "name" : "static-help"
        "response" : {
            "app" : {
                "name" : "client-frontend",
                "description" : "Client Static"
            },
            "build" : {
                "version" : "2.55.8.0",
                "name" : "client-frontend"
            }
        },
        "status" : 200
    }
}

And I'm trying to decode it with json_decode:我正在尝试使用 json_decode 对其进行解码:

<?php

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, 'https://domainname/api/aggregate/info');
    $json = curl_exec($ch);
    curl_close($ch);

    $data = json_decode($json, true);
        echo $data['reports']['name']." | ".$data['reports']['response']['build']['version']."<br \>";
        echo $data['static-application']['name']." | ".$data['static-application']['response']['build']['version']."<br \>";
        echo $data['static-help']['name']." | ".$data['static-help']['response']['build']['version'];
?>

In return i have:作为回报,我有:

reports | 2.55.10.0
static-application | 2.55.10.0
static-help | 2.55.8.0

Everything works, but when the service will return more modules, I will have to manually add new echo sections.一切正常,但是当服务返回更多模块时,我将不得不手动添加新的 echo 部分。

How can I write the same in a loop?如何在循环中编写相同的内容?

You may use a foreach loop to iterate over all $data 's modules:您可以使用foreach循环遍历所有$data的模块:

$data = json_decode($json, true);
foreach($data as $module => $data)
{
    if(isset($data[$module]['name']) && !empty($data[$module]['response']['build']['version']))
    {
        echo $data[$module]['name']." | ".$data[$module]['response']['build']['version']."<br \>";
    }
}

The foreach let you iterate a single element at a time, without the need to manually handle how many elements it contains. foreach允许您一次迭代单个元素,而无需手动处理它包含多少元素。

The empty function checks for the presence of the key in the array and if it contains, in the case it may be missed or empty. empty的 function 检查数组中是否存在键,如果包含,则可能丢失或为空。

If you want to dynamically output the name and build version of the JSON's sections you can use a foreach() loop to iterate over all items:如果您想动态 output 的name和 JSON 部分的build version ,您可以使用foreach()循环遍历所有项目:

$data = json_decode($json);

foreach ( $data as $key => $jsonData ) {
  echo $jsonData->name;
  echo " | ";
  echo $jsonData->response->build->version;
}

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

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