Deserialize JSON to 2 different models

前端 未结 9 1201
眼角桃花
眼角桃花 2021-02-06 23:42

Does Newtonsoft.JSON library have a simple way I can automatically deserialize JSON into 2 different Models/classes?

For example I get the JSON:

[{
  \"g         


        
相关标签:
9条回答
  • 2021-02-07 00:08

    Since both your objects are the same, wouldn't it make more sense to just have an ID/Name structure of a single base class? If you need to send all the data at the same time, you can restructure your data and use a data transfer object pattern. The JSON object would become

    [{
      "guardian": {
        "id": "1453",
        "name": "Foo Bar"
      },
      "patient": {
        "id" : "938",
        "name": "Foo Bar"
      }
    }]
    

    And your corresponding data objects would be:

    public class Record {
      public int id { get; set; } // or string.  I'm not sure which would be more appropriate
      public string name { get; set;}
    }
    

    and

    public class RecordDto {
      public Record guardian { get; set; }
      public Record patient { get; set; }
    }
    

    And your API would receive a

    List<RecordDto>
    

    parameter (since you are passing an array of objects).

    0 讨论(0)
  • 2021-02-07 00:10

    It you want to do it with 1 call, you need to create a class that matches the JSON. That class can then return Guardian and Patient objects as needed. Also you'll need to use an array or list for the return type because the source JSON is an array.

    The class to create:

    public class Pair
    {
        public Pair()
        {
            Guardian = new Guardian();
            Patient = new Patient();
        }
    
        [JsonIgnore]
        public Guardian Guardian { get; set; }
    
        [JsonIgnore]
        public Patient Patient { get; set; }
    
        [JsonProperty(PropertyName = "guardian_id")]
        public int GuardianID
        {
            get { return Guardian.ID; }
            set { Guardian.ID = value; }
        }
    
        [JsonProperty(PropertyName = "guardian_name")]
        public string GuardianName
        {
            get { return Guardian.Name; }
            set { Guardian.Name = value; }
        }
    
        [JsonProperty(PropertyName = "patient_id")]
        public int PatientID
        {
            get { return Patient.ID; }
            set { Patient.ID = value; }
        }
    
        [JsonProperty(PropertyName = "patient_name")]
        public string PatientName
        {
            get { return Patient.Name; }
            set { Patient.Name = value; }
        }
    }
    

    And how to use it:

    var pairs = JsonConvert.DeserializeObject<Pair[]>(response.Content);
    
    if (pairs.Any())
    {
        var pair = pairs[0];
        Console.WriteLine(pair.Guardian.Name);
        Console.WriteLine(pair.Patient.Name);
    }
    
    0 讨论(0)
  • 2021-02-07 00:10
        static void Main(string[] args)
        {
            string json = JsonConvert.SerializeObject(new[] 
            {
                new
                {
                    guardian_id = "1453",
                    guardian_name = "Foo Bar",
                    patient_id = "938",
                    patient_name = "Bar Foo",
                }
            });
    
            Guardian[] guardians = JsonConvert.DeserializeObject<Guardian[]>(json);
            Patient[] patients = JsonConvert.DeserializeObject<Patient[]>(json);
        }
    
    0 讨论(0)
提交回复
热议问题