1

我正在尝试通过 php 从一个相当大的 JSON 文件中提取数据值。该文件来自 USGS 服务器,包含大量我不需要的信息,我只是想获取数据值而不是所有元数据。我从来没有处理过这么大的 JSON 文件,因为我对它们的格式比较陌生。

{
"name": "ns1:timeSeriesResponseType",
"declaredType": "org.cuahsi.waterml.TimeSeriesResponseType",
"scope": "javax.xml.bind.JAXBElement$GlobalScope",
"value": {
    "queryInfo": {
        "queryURL": "http://waterservices.usgs.gov/nwis/iv/format=json&sites=01129500&startDT=2017-11-05T18:00-0500&endDT=2017-11-06T03:00-0500&parameterCd=00060&siteStatus=all",
        "criteria": {
            "locationParam": "[ALL:01129500]",
            "variableParam": "[00060]",
            "timeParam": {
                "beginDateTime": "2017-11-05T23:00:00.000",
                "endDateTime": "2017-11-06T08:00:00.000"
            },
            "parameter": []
        },
        "note": [
            {
                "value": "[ALL:01129500]",
                "title": "filter:sites"
            },
            {
                "value": "[mode=RANGE, modifiedSince=null] interval={INTERVAL[2017-11-05T23:00:00.000Z/2017-11-06T08:00:00.000Z]}",
                "title": "filter:timeRange"
            },
            {
                "value": "methodIds=[ALL]",
                "title": "filter:methodId"
            },
            {
                "value": "2017-11-06T21:03:28.078Z",
                "title": "requestDT"
            },
            {
                "value": "efbacfd0-c335-11e7-9d73-6cae8b663fb6",
                "title": "requestId"
            },
            {
                "value": "Provisional data are subject to revision. Go to http://waterdata.usgs.gov/nwis/help/?provisional for more information.",
                "title": "disclaimer"
            },
            {
                "value": "vaas01",
                "title": "server"
            }
        ]
    },
    "timeSeries": [
        {
            "sourceInfo": {
                "siteName": "CONNECTICUT RIVER AT NORTH STRATFORD, NH",
                "siteCode": [
                    {
                        "value": "01129500",
                        "network": "NWIS",
                        "agencyCode": "USGS"
                    }
                ],
                "timeZoneInfo": {
                    "defaultTimeZone": {
                        "zoneOffset": "-05:00",
                        "zoneAbbreviation": "EST"
                    },
                    "daylightSavingsTimeZone": {
                        "zoneOffset": "-04:00",
                        "zoneAbbreviation": "EDT"
                    },
                    "siteUsesDaylightSavingsTime": true
                },
                "geoLocation": {
                    "geogLocation": {
                        "srs": "EPSG:4326",
                        "latitude": 44.74977166,
                        "longitude": -71.63120018
                    },
                    "localSiteXY": []
                },
                "note": [],
                "siteType": [],
                "siteProperty": [
                    {
                        "value": "ST",
                        "name": "siteTypeCd"
                    },
                    {
                        "value": "01080101",
                        "name": "hucCd"
                    },
                    {
                        "value": "33",
                        "name": "stateCd"
                    },
                    {
                        "value": "33007",
                        "name": "countyCd"
                    }
                ]
            },
            "variable": {
                "variableCode": [
                    {
                        "value": "00060",
                        "network": "NWIS",
                        "vocabulary": "NWIS:UnitValues",
                        "variableID": 45807197,
                        "default": true
                    }
                ],
                "variableName": "Streamflow, ft³/s",
                "variableDescription": "Discharge, cubic feet per second",
                "valueType": "Derived Value",
                "unit": {
                    "unitCode": "ft3/s"
                },
                "options": {
                    "option": [
                        {
                            "name": "Statistic",
                            "optionCode": "00000"
                        }
                    ]
                },
                "note": [],
                "noDataValue": -999999,
                "variableProperty": [],
                "oid": "45807197"
            },
            "values": [
                {
                    "value": [
                        {
                            "value": "2550",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T18:00:00.000-05:00"
                        },
                        {
                            "value": "2550",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T18:15:00.000-05:00"
                        },
                        {
                            "value": "2520",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T18:30:00.000-05:00"
                        },
                        {
                            "value": "2520",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T18:45:00.000-05:00"
                        },
                        {
                            "value": "2520",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T19:00:00.000-05:00"
                        },
                        {
                            "value": "2520",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T19:15:00.000-05:00"
                        },
                        {
                            "value": "2500",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T19:30:00.000-05:00"
                        },
                        {
                            "value": "2500",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T19:45:00.000-05:00"
                        },
                        {
                            "value": "2500",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T20:00:00.000-05:00"
                        },
                        {
                            "value": "2500",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T20:15:00.000-05:00"
                        },
                        {
                            "value": "2500",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T20:30:00.000-05:00"
                        },
                        {
                            "value": "2490",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T20:45:00.000-05:00"
                        },
                        {
                            "value": "2490",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T21:00:00.000-05:00"
                        },
                        {
                            "value": "2490",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T21:15:00.000-05:00"
                        },
                        {
                            "value": "2490",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T21:30:00.000-05:00"
                        },
                        {
                            "value": "2490",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T21:45:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T22:00:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T22:15:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T22:30:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T22:45:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T23:00:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T23:15:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T23:30:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-05T23:45:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T00:00:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T00:15:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T00:30:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T00:45:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T01:00:00.000-05:00"
                        },
                        {
                            "value": "2450",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T01:15:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T01:30:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T01:45:00.000-05:00"
                        },
                        {
                            "value": "2470",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T02:00:00.000-05:00"
                        },
                        {
                            "value": "2490",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T02:15:00.000-05:00"
                        },
                        {
                            "value": "2490",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T02:30:00.000-05:00"
                        },
                        {
                            "value": "2500",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T02:45:00.000-05:00"
                        },
                        {
                            "value": "2500",
                            "qualifiers": [
                                "P"
                            ],
                            "dateTime": "2017-11-06T03:00:00.000-05:00"
                        }
                    ],
                    "qualifier": [
                        {
                            "qualifierCode": "P",
                            "qualifierDescription": "Provisional data subject to revision.",
                            "qualifierID": 0,
                            "network": "NWIS",
                            "vocabulary": "uv_rmk_cd"
                        }
                    ],
                    "qualityControlLevel": [],
                    "method": [
                        {
                            "methodDescription": "",
                            "methodID": 66577
                        }
                    ],
                    "source": [],
                    "offset": [],
                    "sample": [],
                    "censorCode": []
                }
            ],
            "name": "USGS:01129500:00060:00000"
        }
    ]
},
"nil": false,
"globalScope": true,
"typeSubstituted": false

}

如何获取带有“值”标签的所有信息并将这些数字存储到数据结构中,然后我可以在其上执行功能?

谢谢

4

2 回答 2

1

这个问题与繁忙的解析无关,而且 json 本身并没有那么大。你可以迭代$data['value']['timeSeries'][0]['values'][0]['value']并提取你需要的一切。

$data = json_decode($json, true);
$t = [];
foreach ($data['value']['timeSeries'][0]['values'][0]['value'] as $item) {
    $t[] = $item['value'];
}

如果您需要不同的键,只需查看 json 数据并修改循环。如果您需要唯一键,请array_unique$tvar 上使用。

于 2017-11-06T22:23:46.237 回答
0

天真的事情是一个简单的递归操作:

$arr = json_decode($json,true);


$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr), RecursiveIteratorIterator::SELF_FIRST);
$values = [];

foreach ( $iterator as $key => $value ) {

    if ($key === "value" && !is_array($value)) {
        $values[] = $value;   
    }
}

示例:http ://sandbox.onlinephpfunctions.com/code/b36d70d84926d9b5ca9989c9ba96b34fe1f2a7b6

于 2017-11-06T22:25:15.150 回答