我正在尝试解析一个大JSON,我只需要其中的一些东西。我一直在尝试美化它,但我遇到了麻烦,因为它似乎并不完全正确。这是website,我试图删除所有不必要的json,最后得到
[
[
"updateGlobalData",
{
"backgroundData":{
"cities":[
{
"type":"city",
"name":"Polis",
"id":186979,
"level":35,
"ownerId":"99253",
"ownerName":"D3vil666",
"ownerAllyId":"0",
"hasTreaties":0,
"actions":[
],
"state":"vacation",
"viewAble":1,
"infestedByPlague":false
},
{
"type":"city",
"name":"London",
"id":378440,
"level":28,
"ownerId":"242906",
"ownerName":"Mattia",
"ownerAllyId":"5541",
"ownerAllyTag":"LORDS",
"hasTreaties":0,
"actions":[
],
"state":"",
"viewAble":2,
"infestedByPlague":false
}
]
}
}
]
]
我要查找的属性是"State"和"ownerName"。State可以是"vacation",如"state":"休假"或"类似"状态:""""这些属性经常重复,因此我只希望只分析它们一次。例如:我想知道Mattia有哪个州,它应该返回"什么都没有"。D3Vil666有哪个州,它应该返回"假期"。
我使用的代码:
Public Class Form1
Public Class GlobalData
Public Class BackgroundData
Public Property Cities As List(Of City)
End Class
Public Class City
Public Property Type As String
Public Property Name As String
Public Property OwnerName As String
Public Property State As String
End Class
Public Property BackgroundDataa As BackgroundData
End Class
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim client As New WebClient()
Dim jsonString As String = client.DownloadString("https://s10-it.ikariam.gameforge.com/?view=updateGlobalData&islandId=1649&backgroundView=island¤tIslandId=1649&actionRequest=90983fef22b312ff7cbd51f0183bc301&ajax=1")
Dim data As List(Of GlobalData) = JsonConvert.DeserializeObject(Of List(Of GlobalData))(jsonString)
Dim state = data(0).BackgroundDataa.Cities.FirstOrDefault(Function(x) x.OwnerName = "Mattia").State
MsgBox(state)
End Sub
End Class
但我得到了错误:
发生序列化异常:"无法将当前JSON数组(例如,[1,2,3])反序列化为类型" Ikariam_VM.Form1 + GlobalData ",因为该类型需要JSON对象(例如,{" name":"value "})才能正确反序列化。若要修复此错误,请将JSON更改为JSON对象(例如,{" name":"value "}),或者将反序列化的类型更改为实现集合接口(例如,ICollection、IList)(如可以从JSON数组反序列化的List)的数组或类型。还可以向该类型添加JsonArrayAttribute,以强制它从JSON数组反序列化。路径"[0]",第1行,位置2。"
我正在为.net6+开发winform应用程序,并使用nuget Newtonsoft包来处理json。
知道吗?谢谢
1条答案
按热度按时间rn0zuynd1#
你不需要任何自定义类来从你发布的json字符串中获取你需要的数据。我没有VB.net编译器,所以我用c#发布我的代码。我希望你能找到人来翻译它到VB.net