System.Text.Json Deserialize nested object from API call - Data is wrapped in parent JSON property

后端 未结 4 1724
春和景丽
春和景丽 2021-01-22 16:40

I have an API JSON response that wraps the data content in a data property, which looks like this:

{ 
   \"d         


        
4条回答
  •  無奈伤痛
    2021-01-22 17:13

    You can create an object like this:

      public class Data
       {
        public string email { get; set; }
        public string mobile { get; set; }
        public int id { get; set; }
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string role { get; set; }
        }
    
       public class RootObject
       {
        public Data data { get; set; }
       }
    

    then

    var data = JsonSerializer.Deserialize(JsonData);
    

    then you can access the data like the following:

    RootObject.Data.email ;
    RootObject.Data.first_name
    

    Also, anytime you need to convert JSON string to C# POCO class you can use a tool like this : http://json2csharp.com/

提交回复
热议问题