反序列化嵌套的 JSON 数组
本文关键字:数组 JSON 嵌套 反序列化 | 更新日期: 2023-09-27 18:34:48
我正在尝试使用 .NET DataContractJsonSerializer
:
{"region_tag": "eu","services": [{"incidents": [],"status": "online","name": "Client","slug": "client"},{"incidents": [],"status": "online","name": "Game","slug": "game"},{"incidents": [],"status": "online","name": "Store","slug": "store"},{"incidents": [],"status": "online","name": "Website","slug": "web"}],"locales": ["en_GB","de_DE","es_ES","fr_FR","it_IT"],"name": "EU West","hostname": "prod.euw1.lol.riotgames.com","slug": "euw"}
我可以成功地反序列化第一部分,其中我得到以下字符串(或字符串[](:
hostname - returns "prod.euw1.lol.riotgames.com"
name - returns "EU West"
region_tag - returns "eu"
slug - "euw"
locales - "en_GB, de_DE, es_ES, fr_FR, it_IT"
但是,我似乎无法反序列化其余的,因为它们都嵌套在另一个属性中。我该怎么做?
有关完整代码,请参阅此处。
但代码是这样的:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
DataContractJsonSerializer shard;
MemoryStream shardms;
void TriggerUpdate(object json)
{
try
{
ResultListBox.Items.Clear();
shard = new DataContractJsonSerializer(typeof(ShardStatus));
shardms = new MemoryStream(Encoding.UTF8.GetBytes(json.ToString()));
ShardStatusRoot.shardupd = (ShardStatus)shard.ReadObject(shardms);
#region ShardStatus
ResultListBox.Items.Add("Hostname: " + ShardStatusRoot.shardupd.hostname);
foreach (string i in ShardStatusRoot.shardupd.locales)
{
ResultListBox.Items.Add("Locales: " + i);
}
ResultListBox.Items.Add("Location: " + ShardStatusRoot.shardupd.name);
ResultListBox.Items.Add("Region Tag: " + ShardStatusRoot.shardupd.region_tag);
ResultListBox.Items.Add("Slug: " + ShardStatusRoot.shardupd.slug);
#endregion
}
catch (Exception e)
{
MessageBox.Show(e.Message + " => " + e.InnerException);
}
}
public class ShardStatusRoot
{
public static ShardStatus shardupd;
}
[DataContract]
public class ShardStatus
{
[DataMember]
public string hostname { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public string region_tag { get; set; }
[DataMember]
public string slug { get; set; }
[DataMember]
public string[] locales { get; set; }
[DataMember]
public ShardService shardserv;
}
[DataContract]
public class ShardService
{
[DataMember]
public string name { get; set; }
[DataMember]
public string slug { get; set; }
[DataMember]
public string status { get; set; }
[DataMember]
public ShardServiceIncident shardservinc;
}
[DataContract]
public class ShardServiceIncident
{
[DataMember]
public bool active { get; set; }
[DataMember]
public string created_at { get; set; }
[DataMember]
public long id { get; set; }
[DataMember]
public ShardServiceIncidentUpdate shardservincupd;
}
[DataContract]
public class ShardServiceIncidentUpdate
{
[DataMember]
public string author { get; set; }
[DataMember]
public string content { get; set; }
[DataMember]
public string created_at { get; set; }
[DataMember]
public long id { get; set; }
[DataMember]
public string severity { get; set; }
[DataMember]
public string updated_at { get; set; }
[DataMember]
public ShardServiceIncidentUpdateTranslation shardservincupdtrans;
}
[DataContract]
public class ShardServiceIncidentUpdateTranslation
{
[DataMember]
public string content { get; set; }
[DataMember]
public string locale { get; set; }
[DataMember]
public string updated_at { get; set; }
}
方法通过组合框中的选择触发。
尝试使用Newtonsoft.Json,它可以帮助你。