"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"
}
}
在我的JSON
中,密钥不是恒定的。我正在从App_Data
读取文件,我正在尝试通过API调用将IFSC
匹配的银行返回给用户
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());
但是 BankList
返回null。
答案 0 :(得分:2)
您可以使用下面的类结构反序列化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; }
}
用法:
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();
输出:
答案 1 :(得分:0)
您可以从词典中获取帮助
JsonConvert.DeserializeObject<IDictionary<string, BankDetails>(yourInput)
由于最外面的对象具有结果参数,所以我建议您使用反序列化类
class ResultParser
{
IDictionary<string, Bankdetails> Result {get; set;}
}
然后
JsonConvert.DeserializeObject<ResultParser>(yourInput)ş