簡體   English   中英

在PHP上以JSON打印檢索到的數據

[英]Print data retrieved in JSON on a PHP

我正在嘗試從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" } } } }

我只是嘗試過,獲取所需的數據應該沒有任何問題。

例如,要獲取數組中第一項的group_id:

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

也許您正在嘗試以數組形式獲取數據? 在這種情況下,將參數true傳遞給json_decode函數:

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

使用foreach循環顯示以下數據:

  • group_id
  • 顯示名稱
  • 說明和
  • 監視器(以逗號分隔的列表)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM