我正在使用VB.Net和Newtonsoft.JSON工具在Json结构上工作。我正在尝试将Json反序列化为类定义。 Json结构具有两个数组。第一个数组只是一个序列号ID(无标签),其中具有多个@属性。 json结构的示例如下所示。
{"142456":[{"@attributes":{"tag1":"Benitez",
"tag2":"1989",
"tag3":"",
"tag4":"tall",
"tag5":"red"
}},
{"@attributes":{"tag1":"Franco",
"tag2":"1984",
"tag3":"",
"tag4":"short",
"tag5":"grey"
}},
{"@attributes":{"tag1":"Jones",
"tag2":"1956",
"tag3":"",
"tag4":"big",
"tag5":"brown"
}}],
"456782":[{"@attributes":{"tag1":"Smith",
"tag2":"1952",
"tag3":"",
"tag4":"small",
"tag5":"green"
}},
{"@attributes":{"tag1":"Walton",
"tag2":"1980",
"tag3":"",
"tag4":"high",
"tag5":"yellow"
}},
{"@attributes":{"tag1":"Kelly",
"tag2":"1978",
"tag3":"",
"tag4":"average",
"tag5":"black"
}}]
<.... continuing with more items>
}
我设计了一个基本的类结构来将Json反序列化为。
Public Class MainClass
Private m_attributes As Attributes
<JsonProperty(PropertyName:="@attributes")> _
Public Property attributes As Attributes
Get
Return m_attributes
End Get
Set(value As Attributes)
m_attributes = value
End Set
End Property
End Class
Public Class Attributes
Private m_tag1 As String
<JsonProperty(PropertyName:="tag1")> _
Public Property tag1() As String
Get
Return m_tag1
End Get
Set(value As String)
m_tag1 = value
End Set
End Property
Private m_tag2 As String
<JsonProperty(PropertyName:="tag2")> _
Public Property tag2() As String
Get
Return m_tag2
End Get
Set(value As String)
m_tag2 = value
End Set
End Property
:
:
End Class
如果我从Json字符串中删除第一个数组(即排除序列号ID),并且只具有@attributes,则可以使用此属性返回一个JSON数组。
Dim JSONObj = JsonConvert.DeserializeObject(Of MainClass())(Json)
我无法解决的问题是如何重新定义类或JSON对象以能够反序列化两个数组。
答案 0 :(得分:0)
经过各种反复试验,我想到的解决方案是使用一个Dictionary来包含我已经设计的json对象的内部数组。
创建字典:-
auto
希望这对其他人有帮助!