JSON.Net VB deserialize not working
The VB equivalent to
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
is
Dim deserializedProduct as Product = JsonConvert.DeserializeObject(Of Product)(Json)
So I think you want
Dim ThisToken as Token = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Token)(JSonString)
andrew
Updated on June 04, 2022Comments
-
andrew almost 2 years
I'm new to VB and trying to write a webservice that exports and imports JSON.
I'm using JSON.NET 3.5 and can serialize fine:
My Token class is:
<DataContract()> _ Public Class Token <DataMember()> _ Public TokenID As String <DataMember()> _ Public Issued As Date <DataMember()> _ Public Expires As Date <DataMember()> _ Public UserName As String <DataMember()> _ Public CompanyID As String <DataMember()> _ Public ApplicationID As Double <DataMember()> _ Public UserID As Double <DataMember()> _ Public DeviceID As Double <DataMember()> _ Public DeviceSerialNumber As String <DataMember()> _ Public IsValid As Boolean <DataMember()> _ Public DebugText As String
(I started with MS's serialization but thought i'd try JSON.NET)
I serialize with:
Dim ThisToken as New Token ThisToken.DebugText = "blah" and so on
JSONString = Newtonsoft.Json.JsonConvert.SerializeObject(ThisToken)
And I get this output from the webservice:
{"TokenID":"9eaae348-5cbd-46ac-8ba9-83720ac07740","Issued":"/Date(1300422761886+0800)/","Expires":"/Date(1300465961886+0800)/","UserName":"1234","CompanyID":"6","ApplicationID":1.0,"UserID":29.0,"DeviceID":1.0,"DeviceSerialNumber":"9149520800758","IsValid":true,"DebugText":""}
So far so good I think.
To test that deserialization is working, I thought i'd try and deserialise what I just serialised. So I create a webservice that accepts a string and I paste the above into it.
code to deseralise is:
Dim ThisToken As New Token ThisToken = Newtonsoft.Json.JsonConvert.DeserializeObject(JSonString)
When I run the code using VS2005 internal debug/IE testing, I get an http500 internal server error.
I also get the same problem if I try to deserialize immediately after serializing.
I think part of the problem is that the code I was following was c#; from the json.net page:
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
I can see that in c# there is a
<Product>
part which is not readily apparant in vb ?I have no doubt that the newtonsoft json.net product works fine; I'm sure that i'm not doing something right.
Help ?
Andrew