【问题标题】:How to parse JSON data from nested array in PHP如何在 PHP 中解析嵌套数组中的 JSON 数据
【发布时间】:2017-08-13 02:03:24
【问题描述】:

我正在努力从格式如下的 JSON 文件中检索一些值:

{
"@context": [
    "https://raw.githubusercontent.com/geojson/geojson-ld/master/contexts/geojson-base.jsonld",
    {
        "wx": "https://api.weather.gov/ontology#",
        "@vocab": "https://api.weather.gov/ontology#"
    }
],
"type": "FeatureCollection",
"features": [
    {
        "id": "https://api.weather.gov/alerts/NWS-IDP-PROD-2485131-2320093",
        "type": "Feature",
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -95.45,
                        32.36
                    ],
                    [
                        -96.07,
                        32.36
                    ],
                    [
                        -96.08,
                        32.76
                    ],
                    [
                        -95.92,
                        32.82
                    ],
                    [
                        -95.85,
                        32.77
                    ],
                    [
                        -95.77,
                        32.77
                    ],
                    [
                        -95.76,
                        32.75
                    ],
                    [
                        -95.71,
                        32.75
                    ],
                    [
                        -95.66,
                        32.71
                    ],
                    [
                        -95.64,
                        32.72
                    ],
                    [
                        -95.59,
                        32.68
                    ],
                    [
                        -95.6,
                        32.48
                    ],
                    [
                        -95.47,
                        32.37
                    ],
                    [
                        -95.45,
                        32.36
                    ]
                ]
            ]
        },
        "properties": {
            "@id": "https://api.weather.gov/alerts/NWS-IDP-PROD-2485131-2320093",
            "@type": "wx:Alert",
            "id": "NWS-IDP-PROD-2485131-2320093",
            "areaDesc": "Van Zandt",
            "geocode": {
                "UGC": [
                    "TXC467"
                ],
                "SAME": [
                    "048467"
                ]
            },
            "references": [],
            "sent": "2017-08-13T00:03:41+00:00",
            "effective": "2017-08-13T00:03:41+00:00",
            "onset": "2017-08-13T00:03:00+00:00",
            "expires": "2017-08-13T01:00:00+00:00",
            "ends": "2017-08-13T01:00:00+00:00",
            "status": "Actual",
            "messageType": "Alert",
            "category": "Met",
            "severity": "Severe",
            "certainty": "Observed",
            "urgency": "Immediate",
            "event": "Severe Thunderstorm Warning",
            "sender": "NWS Fort Worth TX",
            "headline": "Severe Thunderstorm Warning issued August 12 at 7:03PM CDT expiring August 12 at 8:00PM CDT by NWS Fort Worth TX",
            "description": "The National Weather Service in Fort Worth has issued a\n\n* Severe Thunderstorm Warning for...\nVan Zandt County in north central Texas...\n\n* Until 800 PM CDT.\n\n* At 703 PM CDT, a severe thunderstorm was located near Wills Point,\nmoving east at 25 mph.\n\nHAZARD...65 mph wind gusts and quarter size hail.\n\nSOURCE...Radar indicated.\n\nIMPACT...Hail damage to vehicles is expected. Expect wind damage\nto roofs, siding, and trees.\n\n* This severe thunderstorm will be near,\nCanton around 710 PM CDT.\nEdgewood around 715 PM CDT.\nFruitvale around 725 PM CDT.\nGrand Saline around 735 PM CDT.\nVan around 750 PM CDT.\n\nThis includes Interstate 20 between mile markers 513 and 542.",
            "instruction": "For your protection get inside a sturdy structure and stay away from\nwindows.\n\nContinuous cloud to ground lightning is occurring with this storm.\nMove indoors immediately. Lightning can kill.\n\nHeavy rainfall is occurring with this storm, and may lead to flash\nflooding. Do not drive your vehicle through flooded roadways.",
            "response": "Shelter",
            "parameters": {
                "eventMotionDescription": [
                    "2017-08-13T00:03:00.000-05:00...storm...277DEG...23KT...32.62,-95.97"
                ],
                "hailSize": [
                    "1.00"
                ],
                "windGust": [
                    65
                ],
                "tornadoDetection": [
                    "POSSIBLE"
                ],
                "VTEC": [
                    "/O.NEW.KFWD.SV.W.0313.170813T0003Z-170813T0100Z/"
                ],
                "EAS-ORG": [
                    "WXR"
                ],
                "PIL": [
                    "FWDSVRFWD"
                ],
                "BLOCKCHANNEL": [
                    "CMAS",
                    "EAS",
                    "NWEM"
                ],
                "eventEndingTime": [
                    "2017-08-13T01:00:00Z"
                ]
            }
        }
    },

我正在尝试从“属性”键下的键中获取值。我正在努力解决的是数组是否以嵌套在@context 或“功能”下的“属性”开头?我不熟悉使用 @ 键的 JSON 数据。

我需要更多的价值观。但是对于初学者来说,我只是使用嵌套在“功能”->“属性”下的事件键,其中大多数键是我需要的值。我没有得到输出。

    <?php

$url = 'http://stream.dfwstormforce.com/json/nat_alerts.json'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$result = json_decode($data, true); // decode the JSON feed


foreach($results as $result) {
    dump($result); //this will dump the array
    foreach($results['features'] as $data) {
        dump($data['event']);
    }
}

?>

-谢谢

编辑:为 json_decode 代码添加了建议

【问题讨论】:

  • 看看如果你只是var_dump($results);会发生什么。 features$results 中的键名。它不是$results 内任何$result 值内的任何东西的名称。您需要访问$results['features'],而不是$result['features']
  • 您正在循环一个未定义的变量。你的 var 被称为 result 但你正在循环 results
  • 我已经更新了原始问题中的代码,但仍然没有得到任何输出。

标签: javascript php json loops


【解决方案1】:

正如其他人所说,如果您希望结果是关联数组,则需要将 true 作为第二个参数传递给 json_decode

层次结构是功能/属性/事件,因此您可以查看功能并从每个功能的属性中提取您想要的内容。

<?php
$url = 'http://stream.dfwstormforce.com/json/nat_alerts.json'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$results = json_decode($data, true); // decode the JSON feed

foreach($results['features'] as $currFeature)
{
    $currEvent = $currFeature['properties']['event'];
    echo $currEvent."\n";
}

【讨论】:

  • 这个工作,简化和我正在寻找的。谢谢!而不是打印如何将其分配给变量。当我尝试删除打印并为其创建一个变量并将其回显时,我只是得到数组。
  • 我编辑了代码以显示将事件字符串分配给变量 - 这是您需要的吗?
【解决方案2】:

使用json_decode($data, true)true 告诉 json_decode 返回嵌套的关联数组而不是对象。

【讨论】:

  • 我添加了你的建议,但我仍然没有得到任何数据输出。我是否正确循环了这个?
【解决方案3】:

正如 BarNakedCoder 所说: json_decode($data, true) 但你也需要更换

dump($data['event']

与:

dump($data[0]['event'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多