JSON反序列化问题



我想把这个从API(OpenBank项目)返回的JSON转换成C#。但作为一个新手,我遇到了很多不必要的问题。

{
"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);
}

以下是我正在使用的类(我使用了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);
}

我做错了什么?

您说过您使用了json2csharp,但当我将您的JSON放入json2charp时,我得到了以下内容:

public class Bank
{
public string id { get; set; }
public string short_name { get; set; }
public string full_name { get; set; }
public string logo { get; set; }
public string website { get; set; }
}
public class RootObject
{
public List<Bank> banks { get; set; }
}

然后反序列化为RootObject

string content = reader.ReadToEnd();
var info = JsonConvert.DeserializeObject<RootObject>(content);

你可以在这里看到它的工作原理:https://dotnetfiddle.net/yTcnQh

您需要将bankslist类上的银行列表从banklist重命名为banks,以匹配json节点:

public class bankslist
{
public List<banks> banks { get; set; }
}

相关内容

  • 没有找到相关文章

最新更新