且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Newtonsoft JSON.NET 反序列化动态 Json 字符串

更新时间:2023-01-17 10:47:50

您可以将 JSON 反序列化为 ExpandoObject:

You can deserialize your JSON into an ExpandoObject:

var converter = new ExpandoObjectConverter();
dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json, converter);

它在运行时动态地将成员添加到您的对象中,并允许您对其进行迭代如本答案所述:

Which dynamically adds members to your object at runtime, and allows you to iterate over them as described in this answer:

foreach (var prop in obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
   Console.WriteLine("Name: {0}, Value: {1}",prop.Name, prop.GetValue(obj,null));
}

这样您就可以遍历 obj.message_tags 以获取各个消息,并分别获取它们的所有详细信息.

That way you can iterate over obj.message_tags to get the individual messages, and obtain all their details respectively.