我试图从下面的JSON示例中在vb.net中创建一个类。
我对JSON相当陌生,我只是在找出正确的方法。我已经看过很多如何做到这一点的例子,但它们的格式比我在下面提供的格式要简单得多。
LEVEL0({
"LEVEL1": [
{
"LEVEL2": [
{
"City": "Billings",
"State": "MO",
"Country": "US",
"Id": "1122334455",
"Percent": "39.10"
}
],
"City": "Billings",
"Country": "US",
"NumFound": "1",
"NumReturned": "1",
"State": "MO",
"Status": "Success"
}
],
"Status": "1"
});
我认为这很容易,但是导致我问题的是LEVEL2是一个列表。它可以返回多个城市。所以,它可以在[]中返回这样的东西。
{"City": "Billings","State": "MO","Country": "US","Id": "1122334455","Percent": "39.10"},
{"City": "Fairmount","State": "MN","Country": "US","Id": "1177775","Percent": "64.10",}
所以无论哪种方式该部分都需要是一个列表,但是列表下面的部分也需要成为该类的一部分。你在哪里看到numFound。
我猜这是措辞不好,但我很难找到一个如何做到这一点的好例子。任何方向或建议将不胜感激。我需要正确格式化类,所以当我反序列化到类时,它可以正常工作。
答案 0 :(得分:2)
如果您正在使用某种Web服务,如果您使用的是具有所有这些属性的对象,它应自动为您绑定。类似的东西:
Public Class WrapperClass
Dim Status As Integer
Dim LEVEL1 As New List(Of MiddleClass)
End Class
Public Class MiddleClass
Dim LEVEL2 As New List(Of InnerClass)
Dim City As String
Dim Country As String
Dim NumFound As Integer
Dim NumReturned As Integer
Dim State As String
Dim Status As String
Public Sub MiddleClass()
'init code
End Sub
End Class
Public Class InnerClass
Dim City As String
Dim Country As String
Dim State As String
Dim Id As Integer
Dim Percent As Decimal
End Class
然后在您的网络服务中,让它接受WrapperClass