在VB.Net中将JSon转换为动态对象 [英] Convert JSon to dynamic object in VB.Net
问题描述
我正在使用VB.Net并调用salesforce API.它返回非常难看的JSON
,我无法反序列化.我有一个使用 JSON.Net
I am using VB.Net and calling salesforce API. It returns very ugly JSON
which I am not able to deserialize. I have a following code using JSON.Net
Dim objDescription As Object = JsonConvert.DeserializeObject(Of Object)(result)
objDescription
包含许多属性,在fields
中一个= f.但是当我写类似objDescription.fields
的东西时,它给了我错误.
objDescription
contains many properties, one on=f them in fields
. But when I write something like objDescription.fields
it gives me error.
objDescription.fields Public member 'fields' on type 'JObject' not found. Object
我不是很确定,但是我认为C#允许将任何JSON转换为动态对象.如何在VB.Net
中使用它?
I am not very sure but I think it C# allow to convert any JSON to dynamic object. How can I use it in VB.Net
?
推荐答案
您可以打开Option Strict Off
并使用JSON.NET可以识别的ExpandoObject
.为了利用动态功能,可以使用object类型的变量.
You can turn Option Strict Off
and use ExpandoObject
which is recognized by JSON.NET. In order to leverage the dynamic features you can use a variable of type object.
Option Strict Off
Sub Main
Dim jsonData As Object = JsonConvert.DeserializeObject(Of System.Dynamic.ExpandoObject)("{""Id"":25}")
Dim test As Integer = jsonData.Id
Console.WriteLine(test)
End Sub
如果由于需要JObject的某些功能而希望使用JObject,则可以改为为JObject编制索引.
If you would like to use JObject because you need some of its features, you can index the JObject instead.
Sub Main
Dim jsonData As Object = JsonConvert.DeserializeObject(Of Object)("{""Id"":25}")
Dim test = jsonData("Id")
Console.WriteLine(test)
End Sub
这篇关于在VB.Net中将JSon转换为动态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!