调用和序列化FB feed

时间:2017-01-08 20:21:10

标签: asp.net json vb.net facebook-graph-api

我使用

从多个FB页面调用FB feed
/posts?ids=OI.Plavipingvin,217384491624554&limit=5&fields=message,created_time,id

这是我得到的Feed:

{
   "217384491624554": {
     "data": [
       {
         "message": "Obećanje i zavjet položeni. Dobrodošli u OI Javor ❤",
         "created_time": "2017-01-08T01:05:25+0000",
         "id": "217384491624554_1575515795811410"
       },
       {
         "message": "Zimovanje u punom tijeku :-)",
         "created_time": "2017-01-04T10:06:57+0000",
         "id": "217384491624554_1572127976150192"
       }
     ],
     "paging": {
       "previous": "https://graph.facebook.com/v2.8/217384491624554/posts?fields=message,created_time,id&limit=2&format=json&since=1483837525&access_token=EAACEdEose0cBAMlhIYetCMo0m83Jdo3F7rk4NYmm47Q1T19UDxlKhMQnjDW4Mmelqu3vzTITnVA7E0ZBgl6jDmlHC8J7ZCX4TW2xB0xoHIySu3MK5d9yUWjqMLdUrRab9KTfH1WyzpEfIbxG7JlhPnZACfiFWFfhvO9vrAZCrAZDZD&__paging_token=enc_AdB6GEshkXkXuRJcuiHCF1aoS4rK7Myp3P6mFZAUFeZAPbRVdtmihE7UAOIlFDuTjVKHvmBeiMLmWfIZBfCER7cYrS08kccUDDoixEb2ZABASuwAigZDZD&__previous=1",
       "next": "https://graph.facebook.com/v2.8/217384491624554/posts?fields=message,created_time,id&limit=2&format=json&access_token=EAACEdEose0cBAMlhIYetCMo0m83Jdo3F7rk4NYmm47Q1T19UDxlKhMQnjDW4Mmelqu3vzTITnVA7E0ZBgl6jDmlHC8J7ZCX4TW2xB0xoHIySu3MK5d9yUWjqMLdUrRab9KTfH1WyzpEfIbxG7JlhPnZACfiFWFfhvO9vrAZCrAZDZD&until=1483524417&__paging_token=enc_AdBeiIQZBem7NbobO8r183HtpPnZAOY6CRyehrr8uDJZBXkSS5kKS3YpqdmosFZCGZBobXwMnKW4hEsAIEZCjhYCAL2NdAX7ZCZAyWHZB7GhQCS0IQIqEZBwZDZD"
     }
   },
   "OI.Plavipingvin": {
     "data": [
       {
         "message": "Sretnu novu godinu želi vam Uprava odreda. Budite sretni i zadovoljni. I naravno - pripravni za nove avanture!   ",
         "created_time": "2017-01-02T10:07:27+0000",
         "id": "379925365427474_1274199672666701"
       },
       {
         "message": "Jutros na Omanovcu. Imamo snijeg! :)",
         "created_time": "2016-12-28T07:03:07+0000",
         "id": "379925365427474_1269358063150862"
       }
     ],
     "paging": {
       "previous": "https://graph.facebook.com/v2.8/379925365427474/posts?fields=message,created_time,id&limit=2&format=json&since=1483351647&access_token=EAACEdEose0cBAMlhIYetCMo0m83Jdo3F7rk4NYmm47Q1T19UDxlKhMQnjDW4Mmelqu3vzTITnVA7E0ZBgl6jDmlHC8J7ZCX4TW2xB0xoHIySu3MK5d9yUWjqMLdUrRab9KTfH1WyzpEfIbxG7JlhPnZACfiFWFfhvO9vrAZCrAZDZD&__paging_token=enc_AdDjsccg8E9vHw7XXgXW22NDK0l3MH4mR5XvwXidebNK2Kb8bdewjPiTLGDP8yNw8rpcHYT8VME5YPxLhZC0QZCjdLkHBYJCQZBYdQQWsfmhmC2yQZDZD&__previous=1",
       "next": "https://graph.facebook.com/v2.8/379925365427474/posts?fields=message,created_time,id&limit=2&format=json&access_token=EAACEdEose0cBAMlhIYetCMo0m83Jdo3F7rk4NYmm47Q1T19UDxlKhMQnjDW4Mmelqu3vzTITnVA7E0ZBgl6jDmlHC8J7ZCX4TW2xB0xoHIySu3MK5d9yUWjqMLdUrRab9KTfH1WyzpEfIbxG7JlhPnZACfiFWFfhvO9vrAZCrAZDZD&until=1482908587&__paging_token=enc_AdDZCnhwlRCxibv0aGr141JPdbcHcJssKFjhtToaTpfqKbZABvo5g0fhtCgDpwCNoMBopGK4o0CJxXzRyRJKxLCqOh0belZCXBQdTNZCEF5eRuu6agZDZD"
     }
   }
 }

我目前的FBClass:

Public Class FBData
    Public Property data As New List(Of FBFeed)
End Class

Public Class FBFeed
    Public Property message As String
    Public Property created_time As DateTime
    Public Property id As String
End Class

当前的GetPosts函数,排序和显示结果:

Public Shared Function GetPosts( accessToken As String ) As FBData

    Dim APIlink As String = "https://graph.facebook.com/posts?ids=OI.Plavipingvin,217384491624554&limit=5&fields=message,created_time,id&access_token=" & accessToken

    Dim client As New WebClient()
    client.Encoding = System.Text.Encoding.UTF8
    Dim strJson As [String] = client.DownloadString(APIlink)

    Dim result As FBData = Newtonsoft.Json.JsonConvert.DeserializeObject(Of FBData)( strJson )

    Return result

End Function

Dim array1 As FBData = GetPosts ( accessToken )

For Each Msg As FBFeed In array1.data.OrderByDescending(Function(x) x.created_time)
    Response.Write( i & ". " & Msg.created_time & "<br />" )
Next

我应该使用什么FBClass来序列化这个JSON以及如何读取FBClass列表(或数组)?我不需要用户ID或数据标签。

2 个答案:

答案 0 :(得分:1)

类结构必须如下所示:

Imports Newtonsoft.Json

Public Class FBData
    <JsonProperty(PropertyName:="217384491624554")>
    Public Property Feed_217384491624554 As FBFeed

    <JsonProperty(PropertyName:="OI.Plavipingvin")>
    Public Property Feed_OIPlavipingvin As FBFeed
End Class

Public Class FBFeed
    Public Property data As Datum()
    Public Property paging As Paging
End Class

Public Class Datum
    Public Property message As String
    Public Property created_time As DateTime
    Public Property id As String
End Class

Public Class Paging
    Public Property previous As String
    Public Property [next] As String
End Class

更新:用法:

Dim array1 As FBData = GetPosts ( accessToken )

Dim MyData As New List(Of Datum)
MyData.AddRange(array1.Feed_217384491624554.data)
MyData.AddRange(array1.Feed_OIPlavipingvin.data)

For Each Msg As Datum In MyData.OrderByDescending(Function(x) x.created_time)
    Response.Write(Msg.message & ". " & Msg.created_time & "<br />")
Next

答案 1 :(得分:1)

虽然我的另一个答案有效,但如果有一堆饲料要处理它会有点笨拙,所以这应该是一个更有效的答案。我选择不编辑之前的答案,因为它根据个人需要有效。

鉴于此修订后的课程结构...

Public Class FBFeed
    Public Property data As Datum()
    Public Property paging As Paging
End Class

Public Class Datum
    Public Property message As String
    Public Property created_time As DateTime
    Public Property id As String
End Class

Public Class Paging
    Public Property previous As String
    Public Property [next] As String
End Class

...以及修订后的GetPosts方法......

Public Shared Function GetPosts(accessToken As String, ParamArray args() As String) As Dictionary(Of String, FBFeed)
    'Dim APIlink As String = "https://graph.facebook.com/posts?ids=OI.Plavipingvin,217384491624554&limit=5&fields=message,created_time,id&access_token=" & accessToken

    Dim Ids As String = Join(args, ",")
    Dim APITemplate As String = "https://graph.facebook.com/posts?ids={0}&limit=5&fields=message,created_time,id&access_token={1}"
    Dim APIlink As String = String.Format(APITemplate, Ids, accessToken)

    Using client As New WebClient()
        client.Encoding = Text.Encoding.UTF8
        Dim strJson As String = client.DownloadString(APIlink)

        Return Newtonsoft.Json.JsonConvert.DeserializeObject(Of Dictionary(Of String, FBFeed))(strJson)
    End Using
End Function

...页面请求处理程序中的用法变为......

Dim MyData As New List(Of Datum)

Dim IdList As New List(Of String)
IdList.Add("OI.Plavipingvin")
IdList.Add("217384491624554")

With GetPosts("Access Token Here", IdList.ToArray)
    ' We have a Dictionary(Of String, FBFeed) we can flatten with SelectMany
    '   and consolidate the Datum arrays into the MyData List(Of Datum) above
    MyData.AddRange(.Values.SelectMany(Function(x) x.data).ToList())
End With

For Each Msg As Datum In MyData.OrderByDescending(Function(x) x.created_time)
    Response.Write(Msg.message & ". " & Msg.created_time & "<br />")
Next