Asp.net 如何反序列化facebook数据
我检索了Facebook群组的数据Asp.net 如何反序列化facebook数据,asp.net,vb.net,facebook,facebook-graph-api,Asp.net,Vb.net,Facebook,Facebook Graph Api,我检索了Facebook群组的数据 { "groups": { "data": [ { "name": "Name_1", "unread": 25, "bookmark_order": 13, "id": "000000000001" }, { "name": "Name_2", "unread": 25, "bookmark_orde
{
"groups": {
"data": [
{
"name": "Name_1",
"unread": 25,
"bookmark_order": 13,
"id": "000000000001"
}, {
"name": "Name_2",
"unread": 25,
"bookmark_order": 999999999,
"id": "00000000002"
}
],
"paging": {
"next": "Value_URL"
}
},
"id": "123456"
}
我的尝试:
Public Class groups
Public Property data() As List(Of Facebookgroups)
Get
Return m_data
End Get
Set(value As List(Of Facebookgroups))
m_data = value
End Set
End Property
Private m_data As List(Of Facebookgroups)
End Class
Public Class Facebookgroups
Public Property id() As String
Get
Return m_id
End Get
Set(value As String)
m_id = value
End Set
End Property
Private m_id As String
Public Property name() As String
Get
Return m_name
End Get
Set(value As String)
m_name = value
End Set
End Property
Private m_name As String
End Class
Dim g = FBclient1.Get("me?fields=groups")
Dim facebookgroups As groups = New JavaScriptSerializer().Deserialize(Of groups)(g)
For Each item As Object In facebookgroups.data
Console.WriteLine("id: {0}, name: {1}", item.id, item.name)
Next
我有错误。它表示无法转换为字符串。
这是正确的方法吗?
反序列化对象还有其他解决方法吗?上面编写的输出JSON字符串返回与类不同的结构。你的课程应该是以下几点: 类组,它具有属性数据和id,数据类应该有一个包含属性的类列表:名称、未读、书签顺序、id 编辑: 将其解析为ResponseObject应该可以工作,但我还没有测试过自己 编辑2:工作示例 下面的代码适用于我,我正在从文件c:\dev\tt.json读取您的数据,如果我单步执行,这些组将被正确解析
Sub Main()
Dim g = My.Computer.FileSystem.ReadAllText("c:\dev\tt.json")
Dim facebookgroups As ResponseObject = New JavaScriptSerializer().Deserialize(Of ResponseObject)(g)
End Sub
Public Class Data
Public Property name As String
Public Property unread As Integer
Public Property bookmark_order As Integer
Public Property id As Integer
End Class
Public Class Paging
Public Property [next] As String
End Class
Public Class Groups
Public Property data As List(Of Data)
Public Property paging As Paging
End Class
Public Class ResponseObject
Public Property groups As Groups
Public Property id As Integer
End Class
我使用json2csharp.com来获取课程。然后在VB.net中进行转换 我找到了解决办法。 下面是代码
Public Class Datum
Public Property name() As String
Get
Return m_name
End Get
Set(value As String)
m_name = Value
End Set
End Property
Private m_name As String
Public Property unread() As Integer
Get
Return m_unread
End Get
Set(value As Integer)
m_unread = Value
End Set
End Property
Private m_unread As Integer
Public Property bookmark_order() As Integer
Get
Return m_bookmark_order
End Get
Set(value As Integer)
m_bookmark_order = Value
End Set
End Property
Private m_bookmark_order As Integer
Public Property id() As String
Get
Return m_id
End Get
Set(value As String)
m_id = Value
End Set
End Property
Private m_id As String
Public Property administrator() As System.Nullable(Of Boolean)
Get
Return m_administrator
End Get
Set(value As System.Nullable(Of Boolean))
m_administrator = Value
End Set
End Property
Private m_administrator As System.Nullable(Of Boolean)
End Class
Public Class Paging
Public Property [next]() As String
Get
Return m_next
End Get
Set(value As String)
m_next = Value
End Set
End Property
Private m_next As String
End Class
Public Class Groups
Public Property data() As List(Of Datum)
Get
Return m_data
End Get
Set(value As List(Of Datum))
m_data = Value
End Set
End Property
Private m_data As List(Of Datum)
Public Property paging() As Paging
Get
Return m_paging
End Get
Set(value As Paging)
m_paging = Value
End Set
End Property
Private m_paging As Paging
End Class
Public Class RootObject
Public Property groups() As Groups
Get
Return m_groups
End Get
Set(value As Groups)
m_groups = Value
End Set
End Property
Private m_groups As Groups
Public Property id() As String
Get
Return m_id
End Get
Set(value As String)
m_id = Value
End Set
End Property
Private m_id As String
End Class
Dim gp As RootObject = JsonConvert.DeserializeObject(Of RootObject)(g.ToString)
Dim name As String = gp.groups.data(0).name.ToString
d.innertext=name
无论如何,谢谢你的帮助。你从FBclient.Getme?fields=通话组那里得到了什么?你上面写的字符串?是的。我得到上面的JSON字符串
Public Class Datum
Public Property name() As String
Get
Return m_name
End Get
Set(value As String)
m_name = Value
End Set
End Property
Private m_name As String
Public Property unread() As Integer
Get
Return m_unread
End Get
Set(value As Integer)
m_unread = Value
End Set
End Property
Private m_unread As Integer
Public Property bookmark_order() As Integer
Get
Return m_bookmark_order
End Get
Set(value As Integer)
m_bookmark_order = Value
End Set
End Property
Private m_bookmark_order As Integer
Public Property id() As String
Get
Return m_id
End Get
Set(value As String)
m_id = Value
End Set
End Property
Private m_id As String
Public Property administrator() As System.Nullable(Of Boolean)
Get
Return m_administrator
End Get
Set(value As System.Nullable(Of Boolean))
m_administrator = Value
End Set
End Property
Private m_administrator As System.Nullable(Of Boolean)
End Class
Public Class Paging
Public Property [next]() As String
Get
Return m_next
End Get
Set(value As String)
m_next = Value
End Set
End Property
Private m_next As String
End Class
Public Class Groups
Public Property data() As List(Of Datum)
Get
Return m_data
End Get
Set(value As List(Of Datum))
m_data = Value
End Set
End Property
Private m_data As List(Of Datum)
Public Property paging() As Paging
Get
Return m_paging
End Get
Set(value As Paging)
m_paging = Value
End Set
End Property
Private m_paging As Paging
End Class
Public Class RootObject
Public Property groups() As Groups
Get
Return m_groups
End Get
Set(value As Groups)
m_groups = Value
End Set
End Property
Private m_groups As Groups
Public Property id() As String
Get
Return m_id
End Get
Set(value As String)
m_id = Value
End Set
End Property
Private m_id As String
End Class
Dim gp As RootObject = JsonConvert.DeserializeObject(Of RootObject)(g.ToString)
Dim name As String = gp.groups.data(0).name.ToString
d.innertext=name