我一直在努力尝试和反序列化我从抓住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])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是可以从JSON对象反序列化的集合类型,如数组或List)。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。
路径'数据',第1行,第8位。'
对于解决这个问题的任何帮助表示赞赏,只是没有很多使用VB的newtonsoft实例。
答案 0 :(得分:0)
管理找到一些帮助我的代码!
这是:
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