How to Deserialize a Result Set to a Collection of Objects with .NET

1,131 views
Skip to first unread message

Gary Griggs

unread,
Dec 4, 2013, 11:48:40 PM12/4/13
to orient-...@googlegroups.com
I am attempting to create objects out of the results of a query.  Below is the class:

<JsonObject(MemberSerialization.OptIn)> Public Class User
    <JsonProperty("@class")> Public ReadOnly _class As String = "User"
    <JsonProperty("@rid")> Public ORID As String = ""
    <JsonProperty("@version")> Public version As Integer
    <JsonProperty()> Public UserID As String
End Class


Here is the JSON that is returned:

{"result":[{"@type":"d","@rid":"#-2:1","@version":0,"UserID":"123"}, {"@type":"d","@rid":"#-2:3","@version":0,"UserID":"456"}]}


Here is my code to deserialize using JSON.NET:

Dim mee As List(Of User) = JsonConvert.DeserializeObject(Of List(Of User))(strReturn)

I am attempting to create a list/array/collection of objects (Users, in this case) from the result set of a query.

This is the error message when I deserialize:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[winFlickrFunkServer.User]' 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.


Any assistance with this will be greatly appreciated.

Dwayne W

unread,
Dec 5, 2013, 4:00:09 AM12/5/13
to orient-...@googlegroups.com
Hi Gary, try deserializing to a wrapper class containing  a property named "Result" which is of type List<User>. 

Gary Griggs

unread,
Dec 5, 2013, 8:17:09 AM12/5/13
to orient-...@googlegroups.com
That works!  Sending a virtual pint your way.


I created the following class:

<JsonObject(MemberSerialization.OptIn)> Public Class Users
    <JsonProperty()> Public result As List(Of User)
End Class


and changed the calling code to:

Dim mee As Users = JsonConvert.DeserializeObject(Of Users)(strReturn)
Reply all
Reply to author
Forward
0 new messages