Deserializing JSON using JSon.NET with dynamic data

前端 未结 6 1509
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 11:18

I\'m trying to deserialize some JSON data into objects for an application. Up until now it\'s been fine because the properties on the JSON data was static (key with a value)

6条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 11:49

    The simplest method. In this particular case would probably be to go dynamic.

    dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
    var lastRevId = data.query.pages["6695"].lastrevid;
    

    You can reference any element by it's [] name so you can do something like data["query"]["pages"]["6695"]["lastrevid"]. This will get by all those little objects where the name isn't valid in c#.

提交回复
热议问题