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

随声附和 提交于 2019-12-09 23:22:19

问题


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


回答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)



回答2:


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)


来源:https://stackoverflow.com/questions/13890267/vb-net-deserialize-json-to-anonymous-object-using-newtonsoft-returned-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!