Deserialize JSON to 2 different models

前端 未结 9 1202
眼角桃花
眼角桃花 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-06 23:57

    It can't be done with 1 call with the types that you show. You can try using the generic approach for each type, also you'll need to use arrays or lists for the return type because the source JSON is an array:

    var guardians = JsonConvert.DeserializeObject(response.Content);
    var patients = JsonConvert.DeserializeObject(response.Content);
    

    And then combine the two if you need them to be paired. E.g. if you are sure that you always have just one of each:

    var pair = new Pair(guardians[0], patients[0]);
    

提交回复
热议问题