VB.NET使用newtonsoft返回的错误将JSON反序列化为匿名对象 [英] VB.NET Deserialize JSON to anonymous object using newtonsoft returned error
问题描述
我想将从VB.NET中的服务调用返回的JSON反序列化为匿名类型,但是出现错误.它在使用动态类型的C#中工作,但是我不知道如何在VB中做到这一点.
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.
这是从Web服务调用返回的JSON:
Here is my JSON returned from a web service call:
{"format":"png","height":564,"width":864}
这是我上面分配给param文本的VB代码json:
Here is my VB code json above assigned to param text:
Dim testObj = Newtonsoft.Json.JsonConvert.DeserializeObject(text)
但是当我尝试访问testObj.format时,消息抛出了异常
But when i tried to access testObj.format, an exception was thrown with message
{"Public member 'format' on type 'JObject' not found."}
我已经添加了Option Strict Off
.我不想使用对象/类反序列化JSON.如果在C#中将其分配给动态类型,将可以正常工作.
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.
任何人都可以帮忙吗?我不是VB专家,但是我需要在VB上运行它. TIA
Can anyone please help? I am not expert in VB but I need to have this running on VB. TIA
推荐答案
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)
这篇关于VB.NET使用newtonsoft返回的错误将JSON反序列化为匿名对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!