Anoop Jacob

Anoop Jacob

  • NA
  • 8
  • 2.7k

Deserialize JSON with dynamic keys using C# Json.NET

Sep 3 2020 1:27 AM

From an external API I am receiving the below JSON response

  1. {  
  2.    "bankDetails":[  
  3.       {  
  4.          "ABC Bank":[  
  5.             {  
  6.                "sNo":1,  
  7.                "acNo":"1235465",  
  8.                "acBalance":"100.25"  
  9.             },  
  10.             {  
  11.                "sNo":2,  
  12.                "acNo":"1235467",  
  13.                "acBalance":"50.25"  
  14.             }  
  15.          ],  
  16.          "bankName":"ABC Bank",  
  17.          "totalAmount":"150.50"  
  18.       },  
  19.       {  
  20.          "XYZ Bank":[  
  21.             {  
  22.                "sNo":1,  
  23.                "acNo":"1248565",  
  24.                "acBalance":"75.25"  
  25.             }  
  26.          ],  
  27.          "bankName":"XYZ Bank",  
  28.          "totalAmount":"75.25"  
  29.       },  
  30.       {  
  31.          "BCD Bank":[  
  32.             {  
  33.                "sNo":1,  
  34.                "acNo":"145665",  
  35.                "acBalance":"10.25"  
  36.             },  
  37.             {  
  38.                "sNo":2,  
  39.                "acNo":"195267",  
  40.                "acBalance":"5.25"  
  41.             }  
  42.          ],  
  43.          "bankName":"BCD Bank",  
  44.          "totalAmount":"15.50"  
  45.       }  
  46.    ]  
  47. } 

I need to deserialize this to a C# class using JSON.Net. What should be structure of the C# class as the first key is dynamic?


Answers (6)