我想將這個從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的領導,仍然在這裏學習交易的技巧。 :) –