ibms-dome/BackendWorkerService/Services/Implement/RainApi.cs

129 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
// using QuickType;
//
// var welcome = Welcome.FromJson(jsonString);
namespace RainApi
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
public partial class Welcome
{
public Xml Xml { get; set; }
public Alert Alert { get; set; }
}
public partial class Alert
{
public string Xmlns { get; set; }
public string Identifier { get; set; }
public string Sender { get; set; }
public DateTimeOffset Sent { get; set; }
public string Status { get; set; }
public string MsgType { get; set; }
public string Scope { get; set; }
public string References { get; set; }
//此標籤為將資料統一轉成陣列,因氣象局資料會根據狀況傳回單個物件或陣列,會導致解析失敗
[JsonConverter(typeof(SingleOrArrayConverter<Info>))]
public List<Info> Info { get; set; }
}
public partial class Info
{
public string Language { get; set; }
public string Category { get; set; }
public string Event { get; set; }
public string Urgency { get; set; }
public string Severity { get; set; }
public string Certainty { get; set; }
public EventCode EventCode { get; set; }
public DateTimeOffset Effective { get; set; }
public DateTimeOffset Onset { get; set; }
public DateTimeOffset Expires { get; set; }
public string SenderName { get; set; }
public string Headline { get; set; }
public string Description { get; set; }
public string Instruction { get; set; }
public Uri Web { get; set; }
//此標籤為將資料統一轉成陣列,因氣象局資料會根據狀況傳回單個物件或陣列,會導致解析失敗
[JsonConverter(typeof(SingleOrArrayConverter<EventCode>))]
public List<EventCode> Parameter { get; set; }
public List<Area> Area { get; set; }
}
public partial class Area
{
public string AreaDesc { get; set; }
public EventCode Geocode { get; set; }
}
public partial class EventCode
{
public string ValueName { get; set; }
public string Value { get; set; }
}
public partial class Xml
{
public string Version { get; set; }
public string Encoding { get; set; }
}
public enum ValueName { AlertTitle, ProfileCapTwpEvent10, TaiwanGeocode103 };
//此標籤為將資料統一轉成陣列,因氣象局資料會根據狀況傳回單個物件或陣列,會導致解析失敗
public class SingleOrArrayConverter<T> : JsonConverter
{
// 判斷是否可以轉換該類型,這裡只處理 List<T> 類型
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(List<T>));
}
// 讀取 JSON 並轉換成對應的物件
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var token = JToken.Load(reader); // 讀取 JSON 標記
if (token.Type == JTokenType.Array) // 如果是陣列
{
return token.ToObject<List<T>>(); // 轉換成 List<T>
}
else // 如果不是陣列
{
return new List<T> { token.ToObject<T>() }; // 包裝成單一物件的 List<T>
}
}
// 將物件寫入 JSON
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var list = value as List<T>; // 將值轉換成 List<T>
if (list.Count == 1) // 如果列表中只有一個元素
{
serializer.Serialize(writer, list[0]); // 直接序列化該元素
}
else // 如果列表中有多個元素
{
serializer.Serialize(writer, list); // 序列化整個列表
}
}
}
}