【问题标题】:Print data retrieved in JSON on a PHP在 PHP 上打印以 JSON 格式检索的数据
【发布时间】:2016-02-06 03:46:50
【问题描述】:

我正在尝试从 REST API 检索的数据中打印一个包含监控组列表的表格。结果以 JSON 格式返回。下面是 JSON 数据:

{
    "code": 0,
    "message": "success",
    "data":
    [
        {
            "group_id": "169839000000116001",
            "display_name": "MTI Servers",
            "description": "",
            "monitors":
            [
            ]
        },
        {
            "group_id": "169839000000180001",
            "display_name": "PRB Servers",
            "description": "",
            "monitors":
            [
                "169839000000179003",
                "169839000000176013",
                "169839000000175003",
                "169839000000176007"
            ]
        },
        {
            "group_id": "169839000000046270",
            "display_name": "DB Servers",
            "description": "",
            "monitors":
            [
                "169839000000051011",
                "169839000000047023",
                "169839000000078001"
            ]
        },
        {
            "group_id": "169839000000025200",
            "display_name": "EXT Apps",
            "description": "External Monitoring of Applications",
            "monitors":
            [
                "169839000000025274",
                "169839000000025377",
                "169839000000025359",
                "169839000000025369",
                "169839000000025385",
                "169839000000025226"
            ]
        },
        {
            "group_id": "169839000000025109",
            "display_name": "EXT Services",
            "description": "External monitoring of services.",
            "monitors":
            [
                "169839000000046165",
                "169839000000025256",
                "169839000000025168",
                "169839000000025202",
                "169839000000025189",
                "169839000000025217",
                "169839000000025265"
            ]
        },
        {
            "group_id": "169839000000046015",
            "display_name": "ZMB Servers",
            "description": "",
            "monitors":
            [
                "169839000000050017",
                "169839000000050025",
                "169839000000049001",
                "169839000000050001",
                "169839000000053019",
                "169839000000051003",
                "169839000000050009"
            ]
        },
        {
            "group_id": "169839000000046282",
            "display_name": "NWK Devices",
            "description": "",
            "monitors":
            [
                "169839000000082009",
                "169839000000084077",
                "169839000000084001",
                "169839000000082229"
            ]
        },
        {
            "group_id": "169839000000046013",
            "display_name": "VBR Servers",
            "description": "",
            "monitors":
            [
                "169839000000047007"
            ]
        },
        {
            "group_id": "169839000000054197",
            "display_name": "LNX Servers",
            "description": "",
            "monitors":
            [
                "169839000000060483"
            ]
        },
        {
            "group_id": "169839000000046020",
            "display_name": "VSP Servers",
            "description": "",
            "monitors":
            [
                "169839000000060177",
                "169839000000060170",
                "169839000000060088",
                "169839000000060095",
                "169839000000060102",
                "169839000000060109",
                "169839000000054102"
            ]
        },
        {
            "group_id": "169839000000046058",
            "display_name": "WND Servers",
            "description": "",
            "monitors":
            [
                "169839000000066001",
                "169839000000063119"
            ]
        },
        {
            "group_id": "169839000000128001",
            "display_name": "TPT Servers",
            "description": "",
            "monitors":
            [
                "169839000000143041",
                "169839000000148017",
                "169839000000127035",
                "169839000000123003",
                "169839000000126011",
                "169839000000122011",
                "169839000000129001",
                "169839000000158028"
            ]
        }
    ]
}

我想打印组 ID、名称、描述和属于该组的监视器 ID 列表。我正在使用 json_decode 但我似乎无法从数组中读取正确的变量。

这是我目前的代码:

`<?php
/**
 * Connect to the Site API and extract the list of monitor groups
 */

 // URL to fetch
  $url = "https://www.mymonitorsite.com/api/monitor_groups";

 // Initialize cURL session
  $ch = curl_init($url);

 // Option to Return the Result, rather than just true/false
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 // Set Custom Headers
 $headers = array(
        'Authorization: Authtoken 12345678901234567890123456789012',
        'Content-Type: application/json;charset=UTF-8',
        'Accept: application/json; version=2.0',
 );

 // Option to set the custom headers
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

 // Perform the request, and save content to $result
  $mongrps_json = curl_exec($ch);

 // Close the cURL resource, and free up system resources!
  curl_close($ch);

 // Decode json data into a PHP Array
  $mongrps_array = json_decode($mongrps_json, true);   
?>
   <!DOCTYPE html>
   <html lang="en">
   <head>
   <meta charset="utf-8"/>
   <title>Monitor Group Test</title>
   </head>
   <body>
   <?php
      // List the first monitor group here (test)
      echo "Group ID   = " . $mongrps_array->data[0]->group_id . "<br>";
      echo "Group Name = " . $mongrps_array->data[0]->display_name . <br>";
  ?>
  </body>
  </html>
`

这是 json_decode 的 var_dump:

array(3) { ["code"]=> int(0) ["message"]=> string(7) "success" ["data"]=> array(12) { [0]=> array(4) { ["group_id"]=> string(18) "169839000000116001" ["display_name"]=> string(11) "MTI Servers" ["description"]=> string(0) "" ["monitors"]=> array(0) { } } [1]=> array(4) { ["group_id"]=> string(18) "169839000000180001" ["display_name"]=> string(11) "PRB Servers" ["description"]=> string(0) "" ["monitors"]=> array(4) { [0]=> string(18) "169839000000179003" [1]=> string(18) "169839000000176013" [2]=> string(18) "169839000000175003" [3]=> string(18) "169839000000176007" } } [2]=> array(4) { ["group_id"]=> string(18) "169839000000046270" ["display_name"]=> string(10) "DB Servers" ["description"]=> string(0) "" ["monitors"]=> array(3) { [0]=> string(18) "169839000000051011" [1]=> string(18) "169839000000047023" [2]=> string(18) "169839000000078001" } } [3]=> array(4) { ["group_id"]=> string(18) "169839000000025200" ["display_name"]=> string(8) "EXT Apps" ["description"]=> string(35) "External Monitoring of Applications" ["monitors"]=> array(6) { [0]=> string(18) "169839000000025274" [1]=> string(18) "169839000000025377" [2]=> string(18) "169839000000025359" [3]=> string(18) "169839000000025369" [4]=> string(18) "169839000000025385" [5]=> string(18) "169839000000025226" } } [4]=> array(4) { ["group_id"]=> string(18) "169839000000025109" ["display_name"]=> string(12) "EXT Services" ["description"]=> string(31) "External monitoring of services" ["monitors"]=> array(7) { [0]=> string(18) "169839000000046165" [1]=> string(18) "169839000000025256" [2]=> string(18) "169839000000025168" [3]=> string(18) "169839000000025202" [4]=> string(18) "169839000000025189" [5]=> string(18) "169839000000025217" [6]=> string(18) "169839000000025265" } } [5]=> array(4) { ["group_id"]=> string(18) "169839000000046015" ["display_name"]=> string(11) "ZMB Servers" ["description"]=> string(0) "" ["monitors"]=> array(7) { [0]=> string(18) "169839000000050017" [1]=> string(18) "169839000000050025" [2]=> string(18) "169839000000049001" [3]=> string(18) "169839000000050001" [4]=> string(18) "169839000000053019" [5]=> string(18) "169839000000051003" [6]=> string(18) "169839000000050009" } } [6]=> array(4) { ["group_id"]=> string(18) "169839000000046282" ["display_name"]=> string(11) "NWK Devices" ["description"]=> string(0) "" ["monitors"]=> array(4) { [0]=> string(18) "169839000000082009" [1]=> string(18) "169839000000084077" [2]=> string(18) "169839000000084001" [3]=> string(18) "169839000000082229" } } [7]=> array(4) { ["group_id"]=> string(18) "169839000000046013" ["display_name"]=> string(11) "VBR Servers" ["description"]=> string(0) "" ["monitors"]=> array(1) { [0]=> string(18) "169839000000047007" } } [8]=> array(4) { ["group_id"]=> string(18) "169839000000054197" ["display_name"]=> string(11) "LNX Servers" ["description"]=> string(0) "" ["monitors"]=> array(1) { [0]=> string(18) "169839000000060483" } } [9]=> array(4) { ["group_id"]=> string(18) "169839000000046020" ["display_name"]=> string(11) "VSP Servers" ["description"]=> string(0) "" ["monitors"]=> array(7) { [0]=> string(18) "169839000000060177" [1]=> string(18) "169839000000060170" [2]=> string(18) "169839000000060088" [3]=> string(18) "169839000000060095" [4]=> string(18) "169839000000060102" [5]=> string(18) "169839000000060109" [6]=> string(18) "169839000000054102" } } [10]=> array(4) { ["group_id"]=> string(18) "169839000000046058" ["display_name"]=> string(11) "WND Servers" ["description"]=> string(0) "" ["monitors"]=> array(2) { [0]=> string(18) "169839000000066001" [1]=> string(18) "169839000000063119" } } [11]=> array(4) { ["group_id"]=> string(18) "169839000000128001" ["display_name"]=> string(11) "TPT Servers" ["description"]=> string(0) "" ["monitors"]=> array(8) { [0]=> string(18) "169839000000143041" [1]=> string(18) "169839000000148017" [2]=> string(18) "169839000000127035" [3]=> string(18) "169839000000123003" [4]=> string(18) "169839000000126011" [5]=> string(18) "169839000000122011" [6]=> string(18) "169839000000129001" [7]=> string(18) "169839000000158028" } } } }

【问题讨论】:

    标签: php html json api rest


    【解决方案1】:

    使用foreach循环显示以下数据:

    • group_id
    • display_name
    • 描述和
    • 监视器(以逗号分隔的列表)

      $mongrps_array = json_decode($mongrps_json, true);
      
      foreach($mongrps_array['data'] as $arr){
          foreach($arr as $key => $value){
              if($key == "monitors"){
                  echo $key . ": " . implode(", ", $value) . "<br />";
              }else{
                  echo $key . ": " . $value . "<br />";
              }
          }
          echo "<br />";
      }
      

    【讨论】:

    • @DennisAyala 很高兴我能帮上忙。如果解决了您的问题,请将答案标记为已接受How to accept answer on Stack Overflow?
    • @DennisAyala 您不能为答案投票,但您当然可以通过单击答案左侧的绿色勾号图标接受答案。
    【解决方案2】:

    我刚试了,应该没有问题来获取你要找的数据。

    例如获取数组中第一项的group_id:

    echo $decodedJson->data[0]->group_id;
    

    也许您正试图将数据作为数组获取?在这种情况下,将参数true 传递给json_decode 函数:

    $decodedJson = json_decode($originalJson, true);
    echo $decodedJson['data'][0]['group_id'];
    

    【讨论】:

    • 我将true 参数与json_decode 函数一起使用。我尝试了您的示例,但它根本没有显示任何内容。
    • 试一下var_dump你从json_decode得到的值,如果为null,说明由于某种原因无法解码json。
    猜你喜欢
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多