开发者

Parsing JSON data into list of objects

I'm new at JSON and i am currently struggling with a problem parsing JSON data in a list of objects.

The data that i am t开发者_C百科rying to parse is generated by the facebook graph api, and looks like this :

 {
  "100001621071794": {
    "id": "100001621071794",
    "name": "TEST1",
    "username": "test1",
    "link": "http://www.facebook.com/test1",
    "gender": "male",
    "picture": "http://profile.ak.fbcdn.net/test1.jpg"
    },
  "534237692": {
    "id": "534237692",
    "name": "TEST2",
    "username": "test2",
    "link": "http://www.facebook.com/test2",
    "gender": "female",
    "picture": "http://profile.ak.fbcdn.net/test2.jpg"
   }
}

I am using the following code to parse :

Dim MyFacebookUsers As List(Of FacebookUser) = MyTwitterSerializer.Deserialize(Of List(Of FacebookUser))(FBData)

The class FacebookUser looks like this :

Public Class FacebookUser  
  Public id As String  
  Public name As String  
  Public username As String  
  Public link As String  
  Public gender As String  
  Public picture As String  
End Class  

I know that this is not an array, because it is missing '[' and ']'. But when i replace the '{' and '}' with '[' and ']' i'm getting an error because of an invlaid matrix.

Can someone please point me in the right direction?


You're right, that's not a JSON Array, it's an Object containing FacebookUser Objects where the id value is also the element key.

In .NET the Dictionary class is the JSON Object equivalent, so presumably something like

Dim MyFacebookUsers As List(Of FacebookUser) = MyTwitterSerializer.Deserialize(Of Dictionary(Of FacebookUser))(FBData) 

is what you need.

As you discovered, you can't turn it into a Array (list) by just changing the braces. However, if you actually had an Array, it would look like this:

[
 {"id": "100001621071794",
  "name": "TEST1",
  "username": "test1",
  "link": "http://www.facebook.com/test1",
  "gender": "male",
  "picture": "http://profile.ak.fbcdn.net/test1.jpg"
  },
 {"id": "534237692",
  "name": "TEST2",
  "username": "test2",
  "link": "http://www.facebook.com/test2",
  "gender": "female",
  "picture": "http://profile.ak.fbcdn.net/test2.jpg"
  }
]

and your code would have worked fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜