Deserialize JSON to 2 different models

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

    Not in one call, and it seems the data is an array, so you need a little more work.

    Zip is the key method here to join the two separate object lists:

    Guardian[] guardians = JsonConvert.DeserializeObject(response.Content);
    Patient[] patients = JsonConvert.DeserializeObject(response.Content);
    
    var combined = guardians.Zip(patients, (g, p) => Tuple.Create(g, p)).ToList();
    

    It would be far more easier to just read the JSON at once, it a single object.

提交回复
热议问题