Deserialize JSON to 2 different models

前端 未结 9 1203
眼角桃花
眼角桃花 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: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(json);
            Patient[] patients = JsonConvert.DeserializeObject(json);
        }
    

提交回复
热议问题