我想将这个从API(OpenBank项目)返回的JSON转换为C#。但作为一个新手,我一直有很多不必要的问题。JSON反序列化问题
{
"banks": [
{
"id": "rbs",
"short_name": "The Royal Bank of Scotland",
"full_name": "The Royal Bank of Scotland",
"logo": "http://www.red-bank-shoreditch.com/logo.gif",
"website": "http://www.red-bank-shoreditch.com"
},
{
"id": "test-bank",
"short_name": "TB",
"full_name": "Test Bank",
"logo": null,
"website": null
},
{
"id": "testowy_bank_id",
"short_name": "TB",
"full_name": "Testowy bank",
"logo": null,
"website": null
},
{
"id": "nordea",
"short_name": "Nordea",
"full_name": "Nordea Bank AB",
"logo": "http://logonoid.com/images/nordea-logo.jpg",
"website": "http://www.nordea.com/"
},
{
"id": "nordeaab",
"short_name": "Nordea",
"full_name": "Nordea Bank AB",
"logo": "http://logonoid.com/images/nordea-logo.jpg",
"website": "http://www.nordea.com/"
},
{
"id": "hsbc-test",
"short_name": "HSBC Test",
"full_name": "Hongkong and Shanghai Bank",
"logo": null,
"website": null
},
{
"id": "erste-test",
"short_name": "Erste Bank Test",
"full_name": "Erste Bank Test",
"logo": null,
"website": null
},
{
"id": "deutche-test",
"short_name": "Deutche Bank Test",
"full_name": "Deutche Bank Test",
"logo": null,
"website": null
},
{
"id": "obp-bankx-m",
"short_name": "Bank X",
"full_name": "The Bank of X",
"logo": "https://static.openbankproject.com/images/bankx/bankx_logo.png",
"website": "https://www.example.com"
}
]
}
我已验证JSON字符串,它似乎是正确的。
现在,这是我用来尝试反序列化接收到的内容:
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string content = reader.ReadToEnd();
bankslist info = JsonConvert.DeserializeObject<bankslist>(content);
}
这些类I'm使用(我用这个json2csharp):
public class bankslist
{
public List<banks> banklist { get; set; }
}
public class bankstuff
{
public banks banks;
}
public class banks
{
[JsonProperty(PropertyName = "id")]
public string id { get; set; }
[JsonProperty(PropertyName = "short_name")]
public string short_name { get; set; }
[JsonProperty(PropertyName = "full_name")]
public string full_name { get; set; }
[JsonProperty(PropertyName = "logo")]
public string logo { get; set; }
[JsonProperty(PropertyName = "website")]
public string website { get; set; }
}
似乎我没有在银行课上储存任何我想要的信息。我试过但没有任何资料显示:
foreach (var item in info.banklist)
{
Debug.WriteLine("id=={0} .. full_name=={1} .. website=={2}",
item.id, item.full_name, item.website);
}
我在做什么错?
欢迎堆栈溢出。如果其中一个答案适用于您,请点击答案左边的绿色复选标记将其标记为已接受。当你获得更多的声誉时,你也可以提出你认为有帮助的答案。 –
感谢Brian的领导,仍然在这里学习交易的技巧。 :) –