Mark Cooney
Mark Cooney

Reputation: 121

Facebook JSON in VB.net

This is a string I am getting back from FB

 {
   "id": "5672*****",
   "name": "Mark C*****",
   "first_name": "Mark",
   "last_name": "Cooney",
   "link": "http://www.facebook.com/*****",
   "username": "******",
   "hometown": {
      "id": "111923368834041",
      "name": "Beverley, East Riding of Yorkshire"
   },
   "location": {
      "id": "113198525359593",
      "name": "Littleport, Norfolk, United Kingdom"
   },
   "favorite_teams": [
      {
         "id": "149121281832578",
         "name": "Keep F1 on BBC"
      }
   ],
   "gender": "male",
   "email": "kingte*********",
   "locale": "en_GB",
   "languages": [
      {
         "id": "106059522759137",
         "name": "English"
      }
   ],
   "verified": true,
   "updated_time": "2011-12-07T12:55:03+0000"
}

Obviously starred regions out for security.

I call the top levels like id, first_name, last_name, link etc no problems using this code!!

Private Sub JSONit(ByVal str As String)
    Try

        Dim j As New JSON.JsonObject(str)
        Dim id As String = j("id")
        Dim first_name As String = j("first_name")
        Dim last_name As String = j("last_name")
        Dim link As String = j("link")

        Response.Write(id & ", " & first_name & ", " & last_name & ", " & link)
    Catch ex As Exception
        Response.Write(ex.Message)
    End Try

End Sub

but how can I get the location and hometown information since them seem to be on next level?

Should explain, this is first time I've looked at JSON

Upvotes: 2

Views: 932

Answers (1)

Bill Berlington
Bill Berlington

Reputation: 2414

  1. Convert the JSONObject to a JSONArray. These classes are available in Facebook C# SDK if you are using it.
  2. Iterate the array items one by one to extract the top level items.
  3. Put a check in the loop -

if(arr[i] is JSONArray) { //this is an array and NOT an object of key-value pair. Need to iterate it furthur }

  1. Keep on filling the data items you fetch into a Datatable or something similar.

  2. I would recommend you to write a recursive function for this algorithm.

Upvotes: 1

Related Questions