我如何反序列化Facebook数据

时间:2014-08-18 12:05:41

标签: 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_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

我收到错误。它说不能转换为字符串。 这是正确的方法吗? 反序列化对象还有其他解决方法吗?

2 个答案:

答案 0 :(得分:0)

上面写的输出JSON字符串返回的结构与您的类不同。你的课程应该是:

Class Group,具有属性数据和id,数据类应该包含包含属性的类的List:name,unread,bookmark_order,id。

修改

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

将它解析为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

答案 1 :(得分:0)

我使用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

无论如何感谢你的帮助。