Deserialize Dynamic Json string using Newtonsoft JSON.NET

后端 未结 1 1113
情书的邮戳
情书的邮戳 2020-12-20 12:08

I have a JSON string that I\'m getting from Facebook API, in which I have a node whose name changes according to its content, for example some time it is 45, or 58 etc. It c

相关标签:
1条回答
  • 2020-12-20 12:49

    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));
    }
    

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

    0 讨论(0)
提交回复
热议问题