反序列化foursquare徽章API

时间:2012-02-13 03:52:48

标签: vb.net json foursquare

对于最近的签到,这非常简单:

Dim UserSelfCheckins As FourSquare.UserSelfCheckins.Root
Dim ser As New JavaScriptSerializer()
UserSelfCheckins = ser.Deserialize(Of FourSquare.UserSelfCheckins.Root)(jsonstring)

使用这样的支持类:

Namespace UserSelfCheckins
    Public Class Root
        Public Property meta As MetaProperties
        Public Property response As ResponseProperties
    End Class

    Public Class MetaProperties
        Public Property code As String
    End Class

    Public Class ResponseProperties
        Public Property checkins As UserSelfCheckins
    End Class

    Public Class UserSelfCheckins
        Public Property count As Integer
        Public Property items As List(Of UserSelfCheckinProperties)
    End Class

    Public Class UserSelfCheckinProperties
        Public Property createdAt As Long
        Public Property venue As VenueProperties
    End Class

    Public Class VenueProperties
        Public Property id As String
        Public Property name As String
        Public Property location As LocationProperty
        Public Property categories As List(Of CategoriesProperty)
    End Class

    Public Class LocationProperty
        Public Property city As String
        Public Property state As String
    End Class
End Namespace

您可以使用以下方式显示它们:

 @For Each item In UserSelfCheckins.response.checkins.items
        @<li>
        @item.venue.name
        @item.venue.location.city
        @item.venue.location.state
        </li>
 Next

我无法弄清楚如何为徽章列表执行此操作,因为结构不是常量。这是响应&gt;徽章&gt; [badgeID]&gt; BadgeName。由于BadgeID对于每一个都不同,我在编写匹配的类时遇到了麻烦。

在那之后,我需要找出一种合理的方式来显示实际解锁的徽章。

这是一张来自2个徽章的JSON片段 - 一个解锁。

{
  "meta": {
    "code": 200
  },
  "response": {
    "badges": {
      "4cd0d5e2137c76b04864e9c5": {
        "id": "4cd0d5e2137c76b04864e9c5",
        "badgeId": "4c4f08667a0803bb02212ab7",
        "name": "Groupie",
        "description": "The Backstreet Boys of tech! The Menudo of the interne.... OMG! @NAVEEN JUST TOUCHED MY SHIRT!\n\n",
        "image": {
          "prefix": "https:\/\/plaayfoursquare.s3.amazonaws.com\/badge\/",
          "sizes": [
            57,
            114,
            200,
            300,
            400
          ],
          "name": "\/sxsw2010_groupie.png"
        },
        "unlocks": [
          {
            "checkins": [

            ]
          }
        ]
      },
      "4cb271c2c5e6a1cd61d2e5f6": {
        "id": "4cb271c2c5e6a1cd61d2e5f6",
        "badgeId": "4c4f08667a0803bb17212ab7",
        "name": "Barista",
        "description": "Congrats - you've checked in at 5 different Starbucks! Be sure to pick up a double tall latte for your friend - I'm sure they'd do the same for you.",
        "image": {
          "prefix": "https:\/\/playfoursquare.s3.amazonaws.com\/badge\/",
          "sizes": [
            57,
            114,
            200,
            300,
            400
          ],
          "name": "\/barista.png"
        },
        "unlocks": [
          {
            "checkins": [
              {
                "id": "4cb271c1c5e6a1cd5ed2e5f6",
                "createdAt": 1286762945,
                "type": "checkin",
                "timeZone": "America\/Los_Angeles",
                "venue": {
                  "id": "4b4aa5c0f964a520318c26e3",
                  "name": "Starbucks",
                  "contact": {
                    "twitter": "starbucks"
                  },
                  "location": {
                    "address": "B Gates, Terminal 1",
                    "crossStreet": "LAS Airport",
                    "lat": 36.083164640690136,
                    "lng": -115.1517391204834,
                    "postalCode": "89119",
                    "city": "Las Vegas",
                    "state": "NV",
                    "country": "United States"
                  },
                  "categories": [
                    {
                      "id": "4bf58dd8d48988d1e0931735",
                      "name": "Coffee Shop",
                      "pluralName": "Coffee Shops",
                      "shortName": "Coffee Shop",
                      "icon": {
                        "prefix": "https:\/\/foursquare.com\/img\/categories\/food\/coffeeshop_",
                        "sizes": [
                          32,
                          44,
                          64,
                          88,
                          256
                        ],
                        "name": ".png"
                      },
                      "primary": true
                    }
                  ],
                  "verified": false,
                  "stats": {
                    "checkinsCount": 1401,
                    "usersCount": 1282,
                    "tipCount": 13
                  }
                },
                "photos": {
                  "count": 0,
                  "items": [

                  ]
                },
                "comments": {
                  "count": 0,
                  "items": [

                  ]
                }
              }
            ]
          }
        ]
      }
    },
    "defaultSetType": "foursquare"
  }
}

1 个答案:

答案 0 :(得分:0)

我想我明白了。我缺少的是将徽章级别转换为字符串和对象的字典。

我需要这个模型:

 Namespace UserBadges
    Public Class Root
        Public Property meta As MetaProperties
        Public Property response As ResponseProperties
    End Class

    Public Class MetaProperties
        Public Property code As String
    End Class

    Public Class ResponseProperties
        Public Property badges As Dictionary(Of String, badge)
    End Class

    Public Class badge
        Public Property name As String
        Public Property Description As String
        Public Property Image As BadgeImage
        Public Property unlocks As List(Of Checkins)
    End Class

    Public Class Checkins
        Public Property checkins As List(Of CheckinInfo)
    End Class

    Public Class CheckinInfo
        Public Property createdAt As String
    End Class

    Public Class BadgeImage
        Public Property prefix As String
        Public Property name As String
    End Class

End Namespace

而且:

 UserBadges = ser.Deserialize(FourSquare.UserBadges.Root)(FSQjson)
 For Each item In UserBadges.response.badges
      @item.Value.Image.prefix
      @item.Value.Image.name
      @item.Value.name
      @item.Value.Description     
 Next