Deserialize nested JSON into C# objects

后端 未结 3 1911
野的像风
野的像风 2020-11-27 06:55

I am getting JSON back from an API that looks like this:

{
  \"Items\": {
    \"Item322A\": [{
      \"prop1\": \"string\",
      \"prop2\": \"string\",
             


        
3条回答
  •  星月不相逢
    2020-11-27 07:24

    For "Items" use a Dictionary>, i.e.:

    class Response
    {
        public Dictionary> Items { get; set; }
        public string[] Errors { get; set; }
    }
    
    class Info
    {
        public string Prop1 { get; set; }
        public string Prop2 { get; set; }
        public int Prop3 { get; set; }
        public bool Prop4 { get; set; }
    }
    

    This assumes that the item names "Item322A" and "Item2B" will vary from response to response, and reads these names in as the dictionary keys.

    Sample fiddle.

提交回复
热议问题