0
"Result": 
{
    "AKJB0000001": {
        "BANK": "AKOLA JANATA COMMERCIAL COOPERATIVE BANK",
        "IFSC": "AKJB0000001",
        "BRANCH": "RTGS-HO",
        "ADDRESS": "JANVAIBHAV,OLD COTTON MARKET,AKOLA,PIN 444001",
        "CONTACT": null,
        "CITY": "AKOLA",
        "DISTRICT": "AKOLA",
        "STATE": "MAHARASHTRA"
    },
    "AKJB0000002": {
        "BANK": "AKOLA JANATA COMMERCIAL COOPERATIVE BANK",
        "IFSC": "AKJB0000002",
        "BRANCH": "MALEGAON",
        "ADDRESS": "JANVAIBHAV, MAIN ROAD, MALEGAON PIN 444 503",
        "CONTACT": "271252",
        "CITY": "MALEGAON",
        "DISTRICT": "WASHIM",
        "STATE": "MAHARASHTRA"
    }
}

In my JSON Key is not constant. I am reading file from App_Data and what I am trying to do is returning a IFSC matching bank to user using API call

public async Task<IHttpActionResult> GetBankName(string IFSCCode)
{ 
    // here my code goes
}

string IFSCCodeFile = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/ifsc-codes/" + IFSC + ".json");
JObject IFSCCodes = JObject.Parse(File.ReadAllText(IFSCCodeFile));
BankDetails BankList = JsonConvert.DeserializeObject<BankDetails>(IFSCCodes.ToString());

But BankList is returning null.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • could you please show your `BankDetails` model? – er-sho Dec 15 '18 at 11:51
  • In your `BankDetails` declare `Result` to be a `Dictionary` as shown in [Deserializing JSON when key values are unknown](https://stackoverflow.com/a/24901245/3744182) or [Deserializing JSON with unknown object names](https://stackoverflow.com/q/38688570/3744182) or [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/a/34213724/3744182) or [Parsing JSON Object with variable properties into strongly typed object](https://stackoverflow.com/q/34202496/3744182). – dbc Dec 16 '18 at 20:37

2 Answers2

2

You can use below class structure to deserialize your json.

class Rooot
{
    public Dictionary<string, BankDetail> Result { get; set; }
}

public class BankDetail
{
    public string BANK { get; set; }
    public string IFSC { get; set; }
    public string BRANCH { get; set; }
    public string ADDRESS { get; set; }
    public object CONTACT { get; set; }
    public string CITY { get; set; }
    public string DISTRICT { get; set; }
    public string STATE { get; set; }
}

Usage:

string json = File.ReadAllText(IFSCCodeFile);

Rooot rooot = JsonConvert.DeserializeObject<Rooot>(json);

foreach (var item in rooot.Result)
{
    Console.WriteLine(item.Key);
    Console.WriteLine(item.Value.BANK ?? "NULL");
    Console.WriteLine(item.Value.IFSC ?? "NULL");
    Console.WriteLine(item.Value.BRANCH ?? "NULL");
    Console.WriteLine(item.Value.ADDRESS ?? "NULL");
    Console.WriteLine(item.Value.CONTACT ?? "NULL");
    Console.WriteLine(item.Value.CITY ?? "NULL");
    Console.WriteLine(item.Value.DISTRICT ?? "NULL");
    Console.WriteLine(item.Value.STATE ?? "NULL");
    Console.WriteLine();
}

Console.ReadLine();

Output:

enter image description here

er-sho
  • 9,581
  • 2
  • 13
  • 26
0

you can get help from Dictionary

JsonConvert.DeserializeObject<IDictionary<string, BankDetails>(yourInput)

since your outermost object has result parameter then I suggest you to use class for Deserialization

class ResultParser 
{
    IDictionary<string, Bankdetails> Result {get; set;}

}

then

JsonConvert.DeserializeObject<ResultParser>(yourInput)ş
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72