JSON反序列化问题

时间:2016-05-19 23:24:10

标签: c# json json.net deserialization

我想将从API(OpenBank Project)返回的这个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);
}

我做错了什么?

2 个答案:

答案 0 :(得分:1)

你说过你使用过json2csharp,但是当我把你的JSON放到json2csharp中时我会得到以下结果:

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

答案 1 :(得分:0)

您需要将您的Bankinglist类中的银行列表从银行列表重命名为银行以匹配json节点:

public class bankslist
{
    public List<banks> banks { get; set; }
}
相关问题