簡體   English   中英

需要幫助解析JSON輸出

[英]Need help parsing JSON output

我正在用PHP進行REST調用並返回JSON。 我最終將其放入表中,但現在嘗試輸出該值以了解其價值。

我想進入變量的“數據”字段中有值。 在我的示例中,它們將是"201807", 23.43"201806", 22.54 這是屬性“ data”的前兩個值

我的代碼如下所示:

<?php

$service_url = "http://api.eia.gov/geoset/?geoset_id=ELEC.PRICE.RES.M&regions=USA-AL,USA-AK,USA-AR&api_key=3a8b92cfaf3a21e2e990f228c9152eeb&out=json&start=2018";

$get_data = callAPI('GET', $service_url, false);
$response = json_decode($get_data);

foreach ($response as $r) {
        echo $row->name;
        echo $row->geoset_id;
}

?>

JSON看起來像這樣:

{
    "geoset": {
        "geoset_id": "ELEC.PRICE.RES.M",
        "setname": "Average retail price of electricity : residential : monthly",
        "f": "M",
        "units": "cents per kilowatthour",
        "unitsshort": null,
        "series": {
            "USA-AK": {
                "series_id": "ELEC.PRICE.AK-RES.M",
                "name": "Average retail price of electricity : Alaska : residential : monthly",
                "region": "USA-AK",
                "latlon": null,
                "unitsshort": null,
                "start": "200101",
                "end": "201807",
                "data": [
                    ["201807", 23.43],
                    ["201806", 22.54],
                    ["201805", 22.16],
                    ["201804", 21.61],
                    ["201803", 21.47],
                    ["201802", 21.11],
                    ["201801", 21.67]
                ]
            },
            "USA-AL": {
                "series_id": "ELEC.PRICE.AL-RES.M",
                "name": "Average retail price of electricity : Alabama : residential : monthly",
                "region": "USA-AL",
                "latlon": null,
                "unitsshort": null,
                "start": "200101",
                "end": "201807",
                "data": [
                    ["201807", 12.28],
                    ["201806", 12.41],
                    ["201805", 12.49],
                    ["201804", 12.79],
                    ["201803", 12.65],
                    ["201802", 12.29],
                    ["201801", 11.59]
                ]
            },
            "USA-AR": {
                "series_id": "ELEC.PRICE.AR-RES.M",
                "name": "Average retail price of electricity : Arkansas : residential : monthly",
                "region": "USA-AR",
                "latlon": null,
                "unitsshort": null,
                "start": "200101",
                "end": "201807",
                "data": [
                    ["201807", 9.98],
                    ["201806", 9.99],
                    ["201805", 9.89],
                    ["201804", 10],
                    ["201803", 10.47],
                    ["201802", 9.8],
                    ["201801", 9.36]
                ]
            }
        }
    }
}

到目前為止,我得到了NULL值。

使用json_decode結果將轉換為對象類型。 因此,您應該執行以下操作:

$geoset = $response->geoset; 
$geosetid = $geoset->geoset_id; 

foreach ($geoset->series as $row) {
   echo $row->name;
   $data = $row->data; //this will produce the values as array
}

您的數據數組將如下所示:

Array
(
    [0] => Array
        (
            [0] => 201807
            [1] => 23.43
        )

    [1] => Array
        (
            [0] => 201806
            [1] => 22.54
        )
)
...

希望這會有所幫助! :)

您的代碼將永遠無法工作,因為:

  • 您將$response as $r迭代$response as $r ,但在內部使用$row
  • $response只有一個孩子: geoset 沒有$response->name也沒有$response->geoset_id

如果需要geoset_id ,則必須使用: $response->geoset->geoset_id

為達到這個:

我想進入變量的“數據”字段中有值

使用以下代碼:

foreach ($response->geoset->series as $series) {
    foreach ($series->data as $data) {
        $date = $data[0];
        $value = $data[1];
    }
}

暫無
暫無

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

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