Deserializing JSON into an object with Json.NET

后端 未结 3 915
孤街浪徒
孤街浪徒 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:42

    If you don't want to create a wrapper class you can also access the User this way:

    String jsonString = "{\"user\":{\"user_id\": 1, \"user_type\": \"moderat...";
    JToken root = JObject.Parse(jsonString);
    JToken user = root["user"];
    User deserializedUser = JsonConvert.DeserializeObject(user.ToString());
    

    See this page in the Json.NET doc for details.

提交回复
热议问题