2

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

2 Answers 2

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

Comments

0
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)

Comments

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.