How I deserialize a dynamic json property with RestSharp in C#?

亡梦爱人 提交于 2019-12-06 11:29:28

I'll start with saying that is not elegant at all:

You can get the raw JSON response using

RestResponse response = client.Execute(request);
var jsonString = response.Content; // raw content as string

From there on, you can query it using JSON.NET like that:

var jObject = JObject.Parse(jsonString);
// this could probably be written nicer, but you get the idea...
var resources = jObject ["response"]["users"].Select(t => t["name"]);

resources would then hold a list of your names. This is ugly, inflexible, and I wouldn't recommend it.

Better stick with clever inheritance and custom classes. Its much more readable and your response object will probably not all only have one property, right?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!