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

后端 未结 4 1696
春和景丽
春和景丽 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:05

    Either you get rid of the data object or you can write a custom json Converter. I suggest to change the sent data or just consume it as it is because what you are trying to do is not a best practice.

    0 讨论(0)
  • 2021-01-22 17:10

    It's possible to get a User object using System.Text.Json API without specifying a name of property data from your JSON sample

    { 
       "data":{ 
          "email":"admin@example.com",
          "mobile":"+1555555123",
          "id":4,
          "first_name":"Merchant",
          "last_name":"Vendor",
          "role":"Merchant",
       }
    }
    

    by the following code

    var document = JsonDocument.Parse(json, new JsonDocumentOptions { AllowTrailingCommas = true });
    var enumerator = document.RootElement.EnumerateObject();
    if (enumerator.MoveNext())
    {
        var userJson = enumerator.Current.Value.GetRawText();
        var user = JsonSerializer.Deserialize<User>(userJson,
            new JsonSerializerOptions {AllowTrailingCommas = true});
    }
    

    In the sample above the JsonDocument is loaded, than RootElement is enumerated to get the first nested object as text to deserialize into User instance.

    It's more easier to get a property by name like document.RootElement.GetProperty("data");, but the name can be different actually, according to your question. Accessing through indexer, like document.RootElement[0] is also not possible, because it works only when ValueKind of an element is Array, not the Object, like in your case

    I've also changed the "id":4, to "id":"4",, because getting an error

    Cannot get the value of a token type 'Number' as a string.

    0 讨论(0)
  • 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<RootObject>(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/

    0 讨论(0)
  • 2021-01-22 17:22

    You need to make one another class. That is given below

    Public class UserData
    {
        public User data { get; set; }; 
    }
    

    Now you can Deserialize data using new class called UserData like below

    UserData account = JsonSerializer.Deserialize<UserData>(response.Content, options);
    
    0 讨论(0)
提交回复
热议问题