我一直在努力尝试反序列化我从抓取Instagram中获得的这个json。我四处查看了一下如何做到这一点,但一切都在c#中,转换器可能会错过一些东西。我的代码看起来很正确,但很明显,有一个错误。
这是我想要解析的JSON类型,我希望能够获得用户名和ID的列表,现在想想看,如果我把它做成字典可能会更好?这里是json测试数据:
{
"data":{
"user":{
"edge_follow":{
"count":1,
"page_info":{
"has_next_page":false,
"end_cursor":"AQAVc2MTmWj_GJRRdTbljsZIVf9JPlCXFzZhx2Io0bOJlq_qU-Oxu1Eu9u1HTB64CK8"
},
"edges":[
{
"node":{
"id":"241164259",
"username":"cherylofficial",
"full_name":"Cheryl",
"profile_pic_url":"https://scontent-lhr3-1.cdninstagram.com/t51.2885-19/s150x150/15624120_1236093059810653_247544301280559104_a.jpg",
"is_verified":true,
"followed_by_viewer":false,
"requested_by_viewer":false
}
}
]
}
}
},
"status":"ok"
}
类别:
Public Class Rootobject
Public Property data As Data
Public Property status As String
End Class
Public Class Data
Public Property user As User
End Class
Public Class User
Public Property edge_follow As Edge_Follow
End Class
Public Class Edge_Follow
Public Property count As Integer
Public Property page_info As Page_Info
Public Property edges() As Edge
End Class
Public Class Page_Info
Public Property has_next_page As Boolean
Public Property end_cursor As String
End Class
Public Class Edge
Public Property node As Node
End Class
Public Class Node
Public Property id As String
Public Property username As String
Public Property full_name As String
Public Property profile_pic_url As String
Public Property is_verified As Boolean
Public Property followed_by_viewer As Boolean
Public Property requested_by_viewer As Boolean
End Class
这是我用来获取列表中第一个id的代码:
Dim root As List(Of RootObject) = JsonConvert.DeserializeObject(Of List(Of RootObject))(followingJson)
Dim id As Integer = root(0).data.user.edge_follow.edges(0).node.id
这是我得到的错误:
Newtonsoft.Json.JsonSerializationException:"无法将当前Json对象(例如{"name"value"反序列化为类型"System.Collections.Generic.List`1[Instagram_Login.RootObject]",因为该类型需要Json数组(例如[1,2,3])才能正确反序列化。"。
要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或者更改反序列化的类型,使其成为可以从JSON对象反序列化的普通.NET类型(例如,不是像integer这样的基元类型,也不是像array或List这样的集合类型)。JsonObjectAttribute也可以添加到类型中,以强制它从JSON对象反序列化。
路径"数据",第1行,位置8
对于解决这个问题的任何帮助都是值得赞赏的,只是使用VB.的newtonsoft实例不多
设法找到了一些对我有帮助的代码!
这是:
Public Module JsonHelper
Public Function FromClass(Of T)(data As T, Optional isEmptyToNull As Boolean = False, Optional jsonSettings As JsonSerializerSettings = Nothing) As String
Dim response As String = String.Empty
If Not EqualityComparer(Of T).Default.Equals(data, Nothing) Then
response = JsonConvert.SerializeObject(data, jsonSettings)
End If
Return If(isEmptyToNull, (If(response = "{}", "null", response)), response)
End Function
Public Function ToClass(Of T)(data As String, Optional jsonSettings As JsonSerializerSettings = Nothing) As T
Dim response = Nothing
If Not String.IsNullOrEmpty(data) Then
response = If(jsonSettings Is Nothing, JsonConvert.DeserializeObject(Of T)(data), JsonConvert.DeserializeObject(Of T)(data, jsonSettings))
End If
Return response
End Function
End Module
然后我用这个来获取所有的id
Dim response = JsonHelper.ToClass(Of Rootobject)(followingJson)
Dim count As Integer = response.data.user.edge_follow.count
For i As Integer = 0 To count - 1
Dim id = response.data.user.edge_follow.edges(i).node.id
Console.WriteLine(id.ToString)
Next