Daniel Faria
Daniel Faria

Reputation: 15

JSON deserialization problems

I want to convert this JSON that was returned from an API (OpenBank Project) into C#. But as a newbie, I have been having a ton of unnecessary problems.

{
  "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"
    }
  ]
}

I have verified the JSON string and it seems correct.

Now this is what I used to try to deserialize the content received:

using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    string content = reader.ReadToEnd();
    bankslist info = JsonConvert.DeserializeObject<bankslist>(content);
}

These are the classes I´m using (I used json2csharp for this):

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; }
}

It seems that I am not saving any of the information that I want in the bank class. I tried this but no info is showing:

foreach (var item in info.banklist)
{
    Debug.WriteLine("id=={0} .. full_name=={1} .. website=={2}", 
                    item.id, item.full_name, item.website);
}

What am I doing wrong?

Upvotes: 1

Views: 176

Answers (2)

Craig W.
Craig W.

Reputation: 18155

You said you used json2csharp, but when I put your JSON into json2csharp I get the following:

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; }
}

You then deserialize into the RootObject.

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

You can see it working here: https://dotnetfiddle.net/yTcnQh

Upvotes: 1

abrown
abrown

Reputation: 715

You need to rename the list of banks on your bankslist class from banklist to banks to match the json node:

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

Upvotes: 0

Related Questions