Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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
share|improve this answer
1  
Thank you. I had to change the Rates code to this: Public Class Rates Public AED As Double Public GBP As Double Public USD As Double Public EUR As Double End Class . Thanks you have saved me a lot of time here –  OCDan Feb 8 at 12:29
    
ah, you're right it should be Double instead of Rate. Corrected my answer to prevent misleading people that see this question later. –  har07 Feb 8 at 12:32

you can use

  Dim Rates As Dictionary(Of String, Double)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.