OCDan
OCDan

Reputation: 1153

Parse JSON using JSON.NET VB.NET

I am trying to parse some JSON using JSON.NET; however, it is not something I am familiar with, and I am struggling to deserialize it.

This is the JSON:

{
  "disclaimer": "use at own risk",
  "license": "testing",
  "timestamp": 1391770861,
  "base": "USD",
  "rates": {
    "AED": 3.672839,
    "AFN": 56.367,
    "ALL": 103.5113,
    "AMD": 412.35,
    "ANG": 1.78894,
    "AOA": 97.608324,
    "ARS": 7.880804,
    "AUD": 1.117779,
    "AWG": 1.789825,
    "AZN": 0.784133,
    "BAM": 1.442736,
    "BBD": 2
  }
}

And here is my code currently:

Public Sub ParseJSON()
    JSONResponse = String.Empty
    Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(LatestURL), HttpWebRequest)
    request.ContentType = "application/json; charset=utf-8"
    request.Accept = "application/json, text/javascript, */*"
    request.Method = "POST"
    Using writer As New StreamWriter(request.GetRequestStream())
        writer.Write("{id : 'test'}")
    End Using

    Dim response As WebResponse = request.GetResponse()
    Dim stream As Stream = response.GetResponseStream()

    Using StreamReader As New StreamReader(stream)
        While Not StreamReader.EndOfStream
            JSONResponse += StreamReader.ReadLine()
        End While
    End Using

    Dim RateReply = JsonConvert.DeserializeObject(Of RateReply)(JSONResponse)
End Sub

Public Class RateReply
    Public rates As IList(Of Rate)
    Public base As String
    Public timeStamp As Integer
    Public license As String
    Public disclaimer As String

    Public Sub New()
        rates = New List(Of Rate)()
    End Sub
End Class

Public Class Rate
    Public Currency As String
    Public Rate As Decimal
End Class

The exception I am currently getting is:

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IList`1[KTMOM.Tests.Rate]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

I believe this to be because the rates are not defined as an array in the JSON, however, I do not know how to resolve this.

Any help would be greatly appreciated.

Upvotes: 1

Views: 11615

Answers (2)

Dibu
Dibu

Reputation: 891

you can use

  Dim Rates As Dictionary(Of String, Double)

Upvotes: 1

har07
har07

Reputation: 89295

To resolve the problem you need to either alter JSON format to return array as the error message suggested, or if it isn't possible define another class to represent Rates :

Public Class Rates
    Public AED As Double
    .....
    .....
    Public BBD As Double
End Class

then define rates field in RateReplay class as a Rates :

Public rates As Rates

Upvotes: 2

Related Questions