0

I am trying to build a api-based online currency program for School Project. I did it but I am trying to build a more functional program so I decided to add a second windows form to my program. This second form does currency exchange in past time. The api that I used in my project can do it so it is possible to do but in VB when I try to get currency names in dictionary I get an error like this:

'Newtonsoft.Json.JsonReaderException' türünde bir yakalanamayan özel durum, Newtonsoft.Json.dll öğesinde oluştu Unexpected character encountered while parsing value: {. Path 'rates', line 1, position 43.

And this is my code:

Dim rawResp As String
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader
        Dim jsonResultToDict As Dictionary(Of String, String)
        Dim kurlar As String
        Dim kur As Object
        Dim kurs As String
        request = DirectCast(WebRequest.Create("http://api.fixer.io/" + DateTimePicker1.Value.ToString("yyyy-MM-dd")), HttpWebRequest)
        response = DirectCast(request.GetResponse(), HttpWebResponse)
        reader = New StreamReader(response.GetResponseStream())
        rawResp = reader.ReadToEnd
        jsonResultToDict = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(rawResp)
        kurlar = jsonResultToDict.Item("rates")
        kur = JObject.Parse(kurlar)

By the way I didn't try to set currency names in combobox yet because I couldn't get the data for now.

This is the api : http://fixer.io/

Thanks for helping...

ubt
  • 1

1 Answers1

0

I think because the json data for "rates" doesn't fit into your dictionary model of "String, String", Newtonsoft JSON can't convert from "String, Single" (note how currency code is in double quotes and the rate is not). So, you'll need to create a class object that can be easily converted from JSON.

For example, the class could look like this:

Public Class ForExRates Public base As String Public [date] As String 'enclose var in brackets; Date is a VB keyword. Public rates as Dictionary(Of String, Single)
Public Sub New() End Sub End Class

Then, replace any reference for Dictionary(Of String, String) with ForExRates.

Ok, so your code should like below:

    Dim rawResp As String
    Dim request As HttpWebRequest
    Dim response As HttpWebResponse = Nothing
    Dim reader As StreamReader
    Dim objForExRates As ForExRates

    request = DirectCast(WebRequest.Create("http://api.fixer.io/" + DateTimePicker1.Value.ToString("yyyy-MM-dd")), HttpWebRequest)
    response = DirectCast(request.GetResponse(), HttpWebResponse)
    reader = New StreamReader(response.GetResponseStream())
    rawResp = reader.ReadToEnd
    objForExRates = JsonConvert.DeserializeObject(Of ForExRates)(rawResp)
    Dim sCAD As String = "Rate for 'CAD': " + objForExRates.rates("CAD").ToString()  
cChacon
  • 184
  • 1
  • 8
  • thank you for answering. If I understand right, i need to create a class because the key "rates" has a value of another dictionary. Because when i try to declare it as Dictionary (Of String, Dictionary(Of String,Double)) it throws an error like VB can not resolve it as like that because there is no way to resolve that much variables. Am I right? And I'm new in VB how can I do it in your way? Can you explain as you are explaining a 5 year old baby please :)) – ubt Dec 05 '17 at 20:55
  • @Unal, yes I understand you are new to VB. The answer I gave is as simple as I can give it to you. 1. Just create the class (ForExRates), 2. replace any reference for **Dictionary(Of String, String)** with **ForExRates**. That's it. – cChacon Dec 06 '17 at 05:08
  • Oh figured out that I think I explained myself not right. I am trying to take the **currency names** from the "rates" for inserting into the ComboBox. Because the currency names can change time by time. I did take the rates of currencies. – ubt Dec 08 '17 at 22:56
  • In that case, build a _For Each_ construct as below and use `item.key` to grab the **currency name** and `item.value` to get the **currency rate**: `For Each item As KeyValuePair(Of String, Single) In objForExRates.rates ... Next` – cChacon Dec 08 '17 at 23:24
  • This really worked I'm really grateful to you thank you for your effort and apologize for the misunderstanding. I'm really excited right now :))) – ubt Dec 08 '17 at 23:43
  • Cool! Keep at it! Keep learning! I'm glad to hear I could help you out. – cChacon Dec 09 '17 at 17:03