2012-02-13 56 views
0

對於近期簽入這是非常簡單的:反序列化的foursquare徽章API

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 

什麼我不知道是如何做到這一點的徽章列表結構不恆定。它是Response> Badges> [badgeID]> 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" 
    } 
} 

回答

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