如何反序列化“多级” VB.NET中的JSON? [英] How do I deserialise "multi-level" JSON in VB.NET?

查看:200
本文介绍了如何反序列化“多级” VB.NET中的JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我试图在Visual Basic中反序列化一些JSON。

但是,我遇到了一些麻烦。我正在使用Newtonsoft.Json。

但是当我尝试反序列化一些JSON时,我似乎不断收到此错误:

JSON文本:

'预郎= 文本> { ID: e7cc8ec97b294d2484843d330f136bbd, 名称: iProgramIt, 属性:[{ 名称: 纹理, 值: eyJ0aW1lc3RhbXAiOjE0NzAyMDkwOTIzNzQsInByb2ZpbGVJZCI6ImU3Y2M4ZWM5N2IyOTRkMjQ4NDg0M2QzMzBmMTM2YmJkIiwicHJvZmlsZU5hbWUiOiJpUHJvZ3JhbUl0IiwidGV4dHVyZXMiOnsiU0tJTiI6eyJ1cmwiOiJodHRwOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlL2Y3ZjZmMGU2YTJhM2Q2ZGI2OWE0YWZhOWZlZjI4ZWE0ZDU2ZjM3MTk5Mjc3Zjk1YzFkMmI1ZjBhYWE0ZTIifX19}]}





我试图反序列化时的错误:

 Newtonsoft.Json.JsonSerializationException未处理
HResult = -2146233088
消息=无法将当前JSON数组(例如[1,2,3])反序列化为类型'Minecraft_Report_Generator.Form1 + PropertyX',因为类型需要要正确反序列化的JSON对象(例如{name:value})。
要修复此错误,请将JSON更改为JSON对象(例如{name:value})或将反序列化类型更改为数组或实现集合接口的类型(例如ICollection,IList)像List< T>可以从JSON数组反序列化。 JsonArrayAttribute也可以添加到类型中以强制它从JSON数组反序列化。





以下是我的代码:

 公共  PropertyX 
公共 属性名称作为 字符串
公共 属性作为 字符串
公共 属性签名作为 字符串
结束

公开 Class Ski ns_Base
公开 属性 id As 字符串
公开 属性 name As String
Public 属性属性作为 PropertyX
结束

公共 UUID_basic
公开 属性 id 正如 字符串
公共 属性名称作为 字符串
公共 属性 legacy 作为 布尔
公共 属性 demo 作为 Boolean
结束

Dim retrieveString 作为 字符串 = GETX(Basic_USER &安培; TextBox1.Text)
Dim obj = JsonConvert.DeserializeObject( of UUID_basic)(retrieveString)
Dim SkinsBase As String = GETX( https://sessionserver.mojang.com/session/minecraft/profile/& obj.id)
Dim xObj = JsonConvert.DeserializeObject( of Skins_Base)(SkinsBase)





该代码的最后一行是错误的代码。另外,我从我的代码中提取了与这种情况相关的碎片,所以我意识到你不能从课堂上调用函数等。



谢谢你提前,

-iProgramIt



我的尝试:



我在互联网上试过谷歌搜索,但没有找到与我的情况相符的东西。我试过通过像这样的论坛和其他几个论坛。我找不到任何东西。

解决方案

在JSON中,属性是一个数组(方括号表示一个数组),即使数组中只有一个项目。因此,您的属性字段必须是PropertyX的列表。不知道vb.net我害怕,但我相信它就像List Of PropertyX而不仅仅是作为PropertyX


Hello all,
I am trying to deserialise some JSON in Visual Basic.
However, I am having a bit of trouble. I am using Newtonsoft.Json.
But I seem to keep getting this error when I try to deserialise some JSON:
The JSON text:

{"id":"e7cc8ec97b294d2484843d330f136bbd","name":"iProgramIt","properties":[{"name":"textures","value":"eyJ0aW1lc3RhbXAiOjE0NzAyMDkwOTIzNzQsInByb2ZpbGVJZCI6ImU3Y2M4ZWM5N2IyOTRkMjQ4NDg0M2QzMzBmMTM2YmJkIiwicHJvZmlsZU5hbWUiOiJpUHJvZ3JhbUl0IiwidGV4dHVyZXMiOnsiU0tJTiI6eyJ1cmwiOiJodHRwOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlL2Y3ZjZmMGU2YTJhM2Q2ZGI2OWE0YWZhOWZlZjI4ZWE0ZDU2ZjM3MTk5Mjc3Zjk1YzFkMmI1ZjBhYWE0ZTIifX19"}]}



The Error when I'm trying to deserialise it:

Newtonsoft.Json.JsonSerializationException was unhandled
  HResult=-2146233088
  Message=Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Minecraft_Report_Generator.Form1+PropertyX' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.



The following is my code:

Public Class PropertyX
        Public Property name As String
        Public Property value As String
        Public Property signature As String
    End Class

    Public Class Skins_Base
        Public Property id As String
        Public Property name As String
        Public Property properties As PropertyX
    End Class

    Public Class UUID_basic
        Public Property id As String
        Public Property name As String
        Public Property legacy As Boolean
        Public Property demo As Boolean
    End Class

Dim retrieveString As String = GETX(Basic_USER & TextBox1.Text)
        Dim obj = JsonConvert.DeserializeObject(Of UUID_basic)(retrieveString)
        Dim SkinsBase As String = GETX("https://sessionserver.mojang.com/session/minecraft/profile/" & obj.id)
        Dim xObj = JsonConvert.DeserializeObject(Of Skins_Base)(SkinsBase)



The last line of that code is the one that is errored. Also, I have extracted the bits and pieces from my code relevant to this circumstance, so I realise that you cannot call functions, etc. from the class.

Thanks in advance,
-iProgramIt

What I have tried:

I have tried googling on the internet, but have found nothing matching my circumstance. I have tried looking through forums like this one, and several others. I have found nothing.

解决方案

In the JSON "properties" is an array (the square brackets indicate an array), even if there is only one item in the array. So your properties field needs to be a list of PropertyX. Don't know vb.net I'm afraid but I believe it's something like "as List Of PropertyX" rather than just "as PropertyX"


这篇关于如何反序列化“多级” VB.NET中的JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆