mark vanzuela
mark vanzuela

Reputation: 1391

VB.NET Deserialize JSON to anonymous object using newtonsoft returned error

I would like to deserialize the returned JSON from a service call in VB.NET to an anonymous type but I was having error. It works in C# using dynamic type but i dont know how to do it in VB.

Here is my JSON returned from a web service call:

{"format":"png","height":564,"width":864}

Here is my VB code json above assigned to param text:

Dim testObj = Newtonsoft.Json.JsonConvert.DeserializeObject(text)

But when i tried to access testObj.format, an exception was thrown with message

{"Public member 'format' on type 'JObject' not found."}

I already have added Option Strict Off. I dont want to use an Object/Class to deserialize the JSON. If its in C# assigning this to dynamic type will be working fine.

Can anyone please help? I am not expert in VB but I need to have this running on VB. TIA

Upvotes: 2

Views: 4179

Answers (2)

Anil
Anil

Reputation: 3752

Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
Dim DeSerialObjEventData = New With {.Prop1 = String.Empty, .Prop2 = String.Empty, .Prop3 = String.Empty}...
Dim testObj = js.DeserializeAnnonomusType(source, DeSerialObjEventData)

Upvotes: 0

user2191614
user2191614

Reputation: 11

Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
Dim testObj = js.Deserialize(source, New Object().GetType())

Then you can access the key(attribute name)/values via:

value=testobj(key)

One more thing, you can access your Newtonsoft key(attribute name)/values through:

value=testObj.item(key)

Upvotes: 1

Related Questions