5

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

1 Answer 1

8

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)
Sign up to request clarification or add additional context in comments.

3 Comments

I'll need to register and login and hopefully can then do the vote & mark as complete thing.. andrew
What if I dont have a class/type to deserialize to? Like I have this statement in c#, dynamic deserializedProduct = JsonConvert.DeserializeObject(json); What would be the equivalent of this on VB.NET? see question stackoverflow.com/questions/13890267/…
I think in vb.net you need to create class to deserialize. Otherwise switch to .NET 4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.