Deserializing JSON into an object with Json.NET

后端 未结 3 916
孤街浪徒
孤街浪徒 2020-12-03 05:05

I\'m playing a little bit with the new StackOverflow API. Unfortunately, my JSON is a bit weak, so I need some help.

I\'m trying to deserialize this JSON of a User:<

3条回答
  •  清歌不尽
    2020-12-03 05:36

    Similar to @Alexandre Jasmin's answer, you can use an intermediary JsonSerializer to convert instead of the using the high-level JsonConvert on .ToString(). No idea if it's any more efficient...

    References:

    • https://stackoverflow.com/a/9453198/1037948
    • http://www.west-wind.com/weblog/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing

    Example:

    var root = JObject.Parse(jsonString);
    var serializer = new JsonSerializer();
    var expectedUserObject = serializer.Deserialize(root["user"].CreateReader());
    

提交回复
热议问题