0

I found a few examples but they didn't work. If I can get a array of rates or later to parse into a strongly typed list of rates.

This is what I am working with.

WebRequest webRequest = WebRequest.Create("https://api.exchangeratesapi.io/latest");
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            if (response.StatusDescription == "OK")
            {
                Stream dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);
                string responseFromServer = reader.ReadToEnd();

                JToken rates = JValue.Parse(responseFromServer);
                var ratechart = rates.Values();

            }

Here is the JSON Response result from the webrequest

{"rates":{"CAD":1.516,"HKD":9.2695,"ISK":152.9,"PHP":58.048,"DKK":7.4363,"HUF":366.29,"CZK":26.303,"AUD":1.5565,"RON":4.8813,"SEK":10.1863,"IDR":17184.09,"INR":87.2305,"BRL":6.7979,"RUB":88.8807,"HRK":7.5745,"JPY":129.3,"THB":36.422,"CHF":1.1066,"SGD":1.6008,"PLN":4.5748,"BGN":1.9558,"TRY":8.9502,"CNY":7.7489,"NOK":10.211,"NZD":1.6737,"ZAR":18.2619,"USD":1.1938,"MXN":25.3204,"ILS":3.9606,"GBP":0.863,"KRW":1347.11,"MYR":4.8635},"base":"EUR","date":"2021-03-05"}

Thanks for any help

Soleil
  • 6,404
  • 5
  • 41
  • 61
Zippy
  • 455
  • 1
  • 11
  • 25
  • 1
    Does this answer your question? [Deserialize JSON with C#](https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) – Soleil Mar 06 '21 at 14:14
  • 1
    `var rates = JsonConvert.DeserializeObject>(JToken.Parse(responseFromServer)["rates"].ToString());` – Jimi Mar 06 '21 at 14:37
  • [Deserialize JSON with C#](https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) isn't really a good duplicate, it's too general. A better duplicate is [Deserializing JSON with unknown object names](https://stackoverflow.com/a/38688611/3744182) which specifically answers the case of a dictionary property of a root object. Agree? – dbc Mar 06 '21 at 15:29
  • https://app.quicktype.io/#l=cs&r=json2csharp generates a correct data model for your JSON, it's one of the tools recommended in [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/a/21611680/3744182). – dbc Mar 06 '21 at 15:36
  • Thanks Jimi this worked as I needed. I will need to research more for future json deserialization calls but this is a good start. – Zippy Mar 06 '21 at 15:51

1 Answers1

-1

Following approach might help you!

 HttpResponseMessage response = await client.PostAsync("https://api.exchangeratesapi.io/latest", new StringContent("", Encoding.UTF8, "application/json"));
                if (response.IsSuccessStatusCode)
                {
                    string jsonString = await response.Content.ReadAsStringAsync();

                    var settings = new JsonSerializerSettings
                    {
                        NullValueHandling = NullValueHandling.Ignore,
                        MissingMemberHandling = MissingMemberHandling.Ignore
                    };                 
                                       
                    CLASS_AT_WHICH_JSON_STRING_IS_DESERIALIEZED rates = 
                    CLASS_AT_WHICH_JSON_STRING_IS_DESERIALIEZED();

                    rates =                 
             
                    JsonConvert.DeserializeObject        
                    <CLASS_AT_WHICH_JSON_STRING_IS_DESERIALIEZED>
                    (jsonString, settings);                       
                }
Salahuddin Ahmed
  • 4,854
  • 4
  • 14
  • 35
  • What is `CLASS_AT_WHICH_JSON_STRING_IS_DESERIALIEZED`? Without specifying the class used this doesn't help. Also, why do you allocate an instance of rates by doing `rates = CLASS_AT_WHICH_JSON_STRING_IS_DESERIALIEZED()` then immediately overwrite the value by doing `rates = JsonConvert.DeserializeObject<...`? – dbc Mar 06 '21 at 15:26
  • I meant by CLASS_AT_WHICH_JSON_STRING_IS_DESERIALIEZED as the class which will be acting as the data model whereas source data comes from the called API as the JSON string to be desterilized for the class as it will be specified :) – Salahuddin Ahmed Mar 06 '21 at 15:40