简体   繁体   English

需要帮助解析JSON输出

[英]Need help parsing JSON output

I'm making a REST call in PHP and returning JSON. 我正在用PHP进行REST调用并返回JSON。 I'm eventually putting this into tables, but trying to output the value for now to get the hang of it. 我最终将其放入表中,但现在尝试输出该值以了解其价值。

There are values in the "data" field I would like to get into variables. 我想进入变量的“数据”字段中有值。 In my example they would be "201807", 23.43 and "201806", 22.54 . 在我的示例中,它们将是"201807", 23.43"201806", 22.54 It's the first two values of the property "data" 这是属性“ data”的前两个值

My code looks like this: 我的代码如下所示:

<?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;
}

?>

And JSON looks like this: 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]
                ]
            }
        }
    }
}

So far I'm getting NULL values back. 到目前为止,我得到了NULL值。

As you have used json_decode your result will be convert to object type. 使用json_decode结果将转换为对象类型。 so you should do as follows: 因此,您应该执行以下操作:

$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
}

Your data array will be like this: 您的数据数组将如下所示:

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

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

Hopefully this helps! 希望这会有所帮助! :) :)

Your code will never work, because: 您的代码将永远无法工作,因为:

  • You iterate $response as $r , but use $row inside; 您将$response as $r迭代$response as $r ,但在内部使用$row
  • The $response has only one child: geoset . $response只有一个孩子: geoset There is no $response->name neither $response->geoset_id . 没有$response->name也没有$response->geoset_id

If you want the geoset_id , you must access using: $response->geoset->geoset_id . 如果需要geoset_id ,则必须使用: $response->geoset->geoset_id

To achieve this: 为达到这个:

There are values in the "data" field I would like to get into variables 我想进入变量的“数据”字段中有值

Use the following code: 使用以下代码:

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