This project has moved and is read-only. For the latest updates, please go here.

Deserializing a Json string with array of data in

Jul 20, 2012 at 7:01 AM

Very new to VB.Net and JSON but have a json response from website that I need to break down into its components.

Here is the JSON string.

{"result":[{"UserID":4314,"EmailAddress":"","FirstName":"John","LastName":"Smith","PrinterID":1,"DefaultMultiBusinessID":null,"OnlyShowManifestsFromDefaultMultiBusiness":false,"DefaultCostCenterID":null,"UserCreateDate":"2012-07-05 04:43:41"}],"generated_in":"609ms"}

Here is my JSon class

Public Class JSonObject
    'Public Property result As String
    Public Property UserID As Integer
    Public Property EmailAddress As String
    Public Property FirstName As String
    Public Property LastName As String
    Public Property PrinterID As Integer
    Public Property DefaultMultiBusinessID As String
    Public Property OnlyShowManifestsFromDefaultMultiBusiness As Boolean
    Public Property DefaultCostCenterID As String
    Public Property UserCreateDate As String
    ' Public Property generated_in As String

End Class


Here is the code I'm trying to use to deserialize

  Dim JSonObject As New JSonObject
 Dim deserializedUserID As JSonObject = JsonConvert.DeserializeObject(Of JSonObject)(json)

There is no error messages just empty fields in the deserializedUserID object


I have tried another way but get errors.

Dim jarray = JsonConvert.DeserializeObject(Of List(Of JSonObject))(json)

Here is the error message

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[JSON2.JSonObject]' 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<T>) 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.
Path 'result', line 1, position 10.



As I said very new to VB.Net and JSON.


I have managed to serialize and deserialize a JSon string that is not an array like

{"Name":"Apple","Price":"3.99","sizes":"Small"} but never when it has [] for an array.


Any help would be great.