Javascript 如何在PHP中解析嵌套数组中的JSON数据
我正在努力从如下格式的JSON文件中检索一些值:Javascript 如何在PHP中解析嵌套数组中的JSON数据,javascript,php,json,loops,Javascript,Php,Json,Loops,我正在努力从如下格式的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#"
{
"@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"
]
}
}
},
我试图从“properties”键下的键获取值。我正在努力解决的是,数组是以嵌套在@context下的“properties”开始的还是以“features”开始的?我不熟悉使用@keys的JSON数据
我需要更多的价值观。但对于初学者,我只是使用嵌套在“features”->“properties”下的事件键,其中大多数键用于我需要的值。我没有从中得到输出
<?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解码的代码添加了建议使用json解码($data,true)
。true
告诉json\u decode
返回嵌套的关联数组而不是对象。正如BarNakedCoder所说:
json\u解码($data,true)
但你也需要更换
dump($data['event']
与:
dump($data[0]['event'])
如其他人所述,如果希望结果成为关联数组,则需要将true作为第二个参数传递给 层次结构是features/properties/event,因此您可以查看功能并从每个功能的属性中提取所需内容
<?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";
}
我添加了您的建议,但我仍然没有得到任何要输出的数据。我的循环正确吗?看看如果你只是var\u dump($results)代码><代码>功能
是$results
中的关键字名称。它不是$result
中$result
的任何值内的任何名称。您需要访问$results['features']
,而不是$result['features']
。您正在循环一个未定义的变量。您的var被称为result
,但您正在循环results
我已经更新了我原始问题中的代码,但仍然没有得到任何输出。这很有效,简化了,并且符合我的要求。谢谢如何将其分配给变量,而不是打印它。当我尝试删除打印并为其创建一个变量并回显它时,我只得到一个数组。我编辑了代码以显示将事件字符串分配给变量-这就是您需要的吗?