我正在嘗試決議一個動態復雜的嵌套 JSON 檔案,結果將在陣列中找到特定的鍵。這是一段無效的代碼:
if (input == 'Special Weather Statement') {
var url = "https://api.weather.gov/alerts?zone=AKZ201";
var slide = SlidesApp.getActivePresentation().getSelection().getCurrentPage();
var res = UrlFetchApp.fetch(url);
var content = res.getContentText();
var json = JSON.parse(content);
Logger.log(json);
var event = json['features']['properties']['event'];
if (event == 'Special Weather Statement') {
var shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 20, 259, 420, 60);
var textRange = shape.getText();
var description = json['features'][0]['properties']['description'];
var description = description.replaceAll('\n',' ');
textRange.setText(description);
textRange.getTextStyle().setFontSize(17).setForegroundColor('#ffffff').setFontFamily("Libre Franklin");
問題在于“var event”行。如果我這樣做:
var event = json['features'][0]['properties']['event'];
該代碼僅讀取陣列的第一個塊,我想在其中完全搜索 JSON,找到具有 'event' = 'Special Weather Statement' 的陣列(或任何事件輸入)。
之后,我想將最終輸出排序為具有來自該特定 JSON 中“已發送”鍵的最新事件。
這是 JSON 結構的示例:
{
"@context": [
"https://geojson.org/geojson-ld/geojson-context.jsonld",
{
"@version": "1.1",
"wx": "https://api.weather.gov/ontology#",
"@vocab": "https://api.weather.gov/ontology#"
}
],
"type": "FeatureCollection",
"features": [
{
"id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.1ca69af04faf0efdc58521c0b1b11b10fc0a6cd0.001.1",
"type": "Feature",
"geometry": null,
"properties": {
"@id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.1ca69af04faf0efdc58521c0b1b11b10fc0a6cd0.001.1",
"@type": "wx:Alert",
"id": "urn:oid:2.49.0.1.840.0.1ca69af04faf0efdc58521c0b1b11b10fc0a6cd0.001.1",
"areaDesc": "Western Arctic Coast; Northern Arctic Coast",
"geocode": {
"SAME": [
"002185"
],
"UGC": [
"AKZ201",
"AKZ202"
]
},
"affectedZones": [
"https://api.weather.gov/zones/forecast/AKZ201",
"https://api.weather.gov/zones/forecast/AKZ202"
],
"references": [],
"sent": "2022-10-09T14:23:00-08:00",
"effective": "2022-10-09T14:23:00-08:00",
"onset": "2022-10-09T14:23:00-08:00",
"expires": "2022-10-10T05:00:00-08:00",
"ends": null,
"status": "Actual",
"messageType": "Alert",
"category": "Met",
"severity": "Moderate",
"certainty": "Observed",
"urgency": "Expected",
"event": "Special Weather Statement",
"sender": "[email protected]",
"senderName": "NWS Fairbanks AK",
"headline": "Special Weather Statement issued October 9 at 2:23PM AKDT by NWS Fairbanks AK",
"description": "West winds of 25 to 35 mph blowing over a large fetch of open\nwater over the Arctic Ocean and Chukchi Sea will cause elevated\nwater levels potentially 1 to 3 feet above the highest high tide\nand elevated surf along the Arctic Coast from Tuesday through\nThursday. This will likely lead to beach erosion along the Arctic\nCoast.",
"instruction": null,
"response": "Execute",
"parameters": {
"AWIPSidentifier": [
"SPSNSB"
],
"WMOidentifier": [
"WWAK81 PAFG 092223"
],
"NWSheadline": [
"Elevated Surf Possible Tuesday Through Thursday"
],
"BLOCKCHANNEL": [
"EAS",
"NWEM",
"CMAS"
],
"EAS-ORG": [
"WXR"
]
}
}
},
{
"id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.fb007fa8f54048b448cba44f41ac214a0aae3bb0.001.1",
"type": "Feature",
"geometry": null,
"properties": {
"@id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.fb007fa8f54048b448cba44f41ac214a0aae3bb0.001.1",
"@type": "wx:Alert",
"id": "urn:oid:2.49.0.1.840.0.fb007fa8f54048b448cba44f41ac214a0aae3bb0.001.1",
"areaDesc": "Western Arctic Coast; Northern Arctic Coast; Central Beaufort Sea Coast; Eastern Beaufort Sea Coast",
"geocode": {
"SAME": [
"002185"
],
"UGC": [
"AKZ201",
"AKZ202",
"AKZ203",
"AKZ204"
]
},
"affectedZones": [
"https://api.weather.gov/zones/forecast/AKZ201",
"https://api.weather.gov/zones/forecast/AKZ202",
"https://api.weather.gov/zones/forecast/AKZ203",
"https://api.weather.gov/zones/forecast/AKZ204"
],
"references": [],
"sent": "2022-10-09T06:42:00-08:00",
"effective": "2022-10-09T06:42:00-08:00",
"onset": "2022-10-09T06:42:00-08:00",
"expires": "2022-10-09T16:45:00-08:00",
"ends": null,
"status": "Actual",
"messageType": "Alert",
"category": "Met",
"severity": "Moderate",
"certainty": "Observed",
"urgency": "Expected",
"event": "Special Weather Statement",
"sender": "[email protected]",
"senderName": "NWS Fairbanks AK",
"headline": "Special Weather Statement issued October 9 at 6:42AM AKDT by NWS Fairbanks AK",
"description": "West winds of 25 to 35 mph blowing over a 600 NM fetch of open\nwater over the Arctic Ocean and Chukchi Sea will cause elevated\nwater levels and elevated surf along the Arctic Coast from Monday\nnight through Thursday. This will likely lead to beach erosion\nalong the Arctic Coast Monday night through Thursday.",
"instruction": null,
"response": "Execute",
"parameters": {
"AWIPSidentifier": [
"SPSNSB"
],
"WMOidentifier": [
"WWAK81 PAFG 091442"
],
"NWSheadline": [
"Elevated Surf Possible Monday Night Through Thursday"
],
"BLOCKCHANNEL": [
"EAS",
"NWEM",
"CMAS"
],
"EAS-ORG": [
"WXR"
]
}
}
}
我將不勝感激任何幫助或意見,謝謝!
uj5u.com熱心網友回復:
您可以使用該sort
方法對 features 陣列進行排序,并在獲取有序陣列的第一個元素后:
樣本:
var url = "https://api.weather.gov/alerts?zone=AKZ201";
var res = UrlFetchApp.fetch(url);
var content = res.getContentText();
var json = JSON.parse(content);
const orderedEvents = json.features.sort((a,b)=>{
return new Date(b.properties.sent).getTime() - new Date(a.properties.sent).getTime()
})
console.log(orderedEvents[0].properties.event)
檔案
- 參考:比較日期的方法
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/513576.html
標籤:javascript数组json谷歌幻灯片嵌套的 json