Howard Hee
Howard Hee

Reputation: 929

JSON Deserialize Error

I wanna pass the json to server, below is the json format:

[{"StaffID":"S01","StaffRank":"Manager"},{"StaffID":"S02","StaffRank":"Waiter"}]

After I tried the following code to get the json array:

  Dim request As String = New StreamReader(data).ReadToEnd
    response = AddStaff(JsonConvert.DeserializeObject(Of tbl_Staff)(request))
    Return JsonConvert.SerializeObject(response)

I get the new error which is:

"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'tbl_Staff' because the type requires a JSON object (e.g.{"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array."

What is the problem? Thanks

Upvotes: 0

Views: 1405

Answers (2)

Howard Hee
Howard Hee

Reputation: 929

I found the answer.I just change the code to List. Then working perfectly.

Dim request As String = New StreamReader(data).ReadToEnd
response = AddStaff(JsonConvert.DeserializeObject(Of List(Of tbl_Staff))(request))
Return JsonConvert.SerializeObject(response)

Upvotes: 1

James Newton-King
James Newton-King

Reputation: 49042

The problem is you cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'tbl_Staff' because the type requires a JSON object (e.g.{"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

It seems like no matter how detailed I make that error message, people just don't read it :-\

Upvotes: 1

Related Questions