-1

I have a problem with JSON that is downloading from bank api. I am making some sort of ATM.

        private void get_response()
    {
        WebClient wp = new WebClient();
        string url = "http://api.nbp.pl/api/exchangerates/tables/a/?format=json";
        var response = wp.DownloadString(url);
        var jsonData = System.Text.Json.JsonSerializer.Deserialize<List<Rootobject>>(response);
        Console.WriteLine(jsonData[0].rates.ToString());//ATM.Rates+Rate[]
        Console.WriteLine(jsonData[0].rates[1].currency.ToString());//USD Dolar
        get_data(response);
    }

I was searching a lot and I didn't find answer to my problem. Only that I need to use List but I have no idea how to use it and make it again to json but only that rates table. My Class:

        public class Rootobject
    {
        public Class1[] Property1 { get; set; }
    }

    public class Class1
    {
        public string table { get; set; }
        public string no { get; set; }
        public string effectiveDate { get; set; }
        public Rate[] rates { get; set; }
    }

    public class Rate
    {
        public string currency { get; set; }
        public string code { get; set; }
        public float mid { get; set; }
    }

I need to display Rate table in my App but I have no idea how to get into rates value in JSON string because when i display response this happen.

        private void get_data(string response)
    {
        dataGridView.Rows.Clear();
        DataTable dataTable = (DataTable)JsonConvert.DeserializeObject(response, (typeof(DataTable)));
        dataGridView.DataSource = dataTable;
    }

DataGridViev

And I need to display only that rates. Like that: rates

Kubis10
  • 13
  • 1
  • 6

1 Answers1

1

Part 1. Json to objects

The json is:

[
    {
        "table": "A",
        "no": "083/A/NBP/2021",
        "effectiveDate": "2021-04-30",
        "rates": [
            {
                "currency": "bat (Tajlandia)",
                "code": "THB",
                "mid": 0.1211
            },
            {
                "currency": "dolar amerykański",
                "code": "USD",
                "mid": 3.7746
            },
            (...)
     }
]

It's an array with a single object. Here is my mapping:

public class RateData
{
    public List<Rate> Rates {get; set;}
}

public class Rate
{
    public string Currency { get;set;}
    public string Code {get; set;}
    public decimal Mid {get; set;}
}

Test code:

var options = new JsonSerializerOptions {
    PropertyNameCaseInsensitive = true
};
var list = JsonSerializer.Deserialize<List<RateData>>(json, options);

var single = list.Single(); // throws if there are more
foreach(var rate in single.Rates)
{
    Console.WriteLine($"{rate.Currency}, {rate.Code}, {rate.Mid}");
}

Output:

bat (Tajlandia), THB, 0.1211
dolar amerykański, USD, 3.7746
dolar australijski, AUD, 2.9323
dolar Hongkongu, HKD, 0.4862
...

Part 2. List to DataTable

How to convert a List into DataTable

but I think this may work:

dataGridView.DataSource = single.Rates;

If not then I think this works:

var data = single.Rates
    .Select( r => new { 
        Currency = r.Currency, 
        Code = r.Code, 
        Mid = r.Mid
        });
dataGridView.DataSource = data;

(Based on Binding objects DataGridView C#)

tymtam
  • 31,798
  • 8
  • 86
  • 126