I need your help! Facebook API - JSON Object vs. string

Hey guys,

I need your help. For my university class in Processing I want to map my Facebook friends' photo locations on a world map in order to know where my friends have been on vacation. A friend, my university teacher and the internet ;) helped me knock the code together (actually they did most of it..I wouldn't have thought that it is so complicated, at least for me) but we couldn't find the answer to a problem:

We used JSON Objects and in the Facebook API it is built like that: "place": { "location": { "zip": "", "street": "Walter-carsten- Straße", "longitude": 8.6736867374647, "latitude": 53.759298709308, "city": "Nordholz" },

but sometimes we have the problem that "location" is not a JSON object but a string, like this:

"location": "65 Saturn St SF",

and then the sketch just stops running giving the error that location is not a JSON object. Does any of you have a clue how we could make it work both with JSON Objects and strings? As you might have guessed so far, I'm really a newbie so please be gentle with me ;)

Thanks a lot!!!!

Here is the code:

String access_token = "xxx";

float longitude, latitude;

void setup() {

  size(800, 400);

  JSONObject responsephotos;
  JSONObject responsefriends;
  JSONObject place;


  responsefriends = FB( "me/friends" );


  for(int i=0; i<100; i++) {
    String friend_id = responsefriends.getJSONArray("data").getJSONObject(i).getString("id");


    responsephotos = FB( friend_id + "/photos?fields=source,place" );

    for(int j=0; j<responsephotos.getJSONArray("data").size(); j++) {

       if( responsephotos.getJSONArray("data").getJSONObject(j).hasKey("place")) {

         place = responsephotos.getJSONArray("data").getJSONObject(j);

         println(i);
         println("+++++" + place + "++++++");
         println(place.getJSONObject("place").hasKey("location") );
         println(place.getJSONObject("place").keys() );
         println("@@@@@@@@@@@@@@@@@@@@@@@@");


         if( place.getJSONObject("place").hasKey("location") && 
             place.getJSONObject("place").getJSONObject("location").isNull("longitude") ) { // SOMETIMES JSON OBJECT, SOMETIMES STRINGS -> HOW TO FIND OUT, IF IT IS A STRING

           println("+++++" + place.getJSONObject("place").getJSONObject("location").getFloat("longitude") + "+++++++");
           println("+++++" + place.getJSONObject("place").getJSONObject("location").getFloat("latitude") + "+++++++");
           println("****************************");

           longitude = place.getJSONObject("place").getJSONObject("location").getFloat("longitude");
           latitude = place.getJSONObject("place").getJSONObject("location").getFloat("latitude");

           longitude = map(longitude, -180, 180, 0, width);
           latitude = map(latitude, -90, 90, 0, height);

           fill(255);
           ellipse(longitude, latitude, 5, 5);
         }

       }

       else 
       { 
         //println("NO LOCATION" + i); 
       }
    }
  }

}


void draw() {

}


JSONObject FB( String request ) {

  String json_string = "";
  String connector = "?";
  if( request.indexOf("?") != -1 ) {
    connector = "&";
  }

  String response[] = loadStrings("https://graph.facebook.com/" + request + connector + "access_token=" + access_token);

  for(int i=0;i<response.length;i++) {
    json_string += response[i];
  }

  JSONObject json = JSONObject.parse( json_string );

//  println( "==============" );
//  println( request );
//  println( json );
//  println( "==============" );

  return json;

}

Answers

  • I fear the current Json implementation in Processing doesn't offer a way to know what kind of data is available at a given key. It assumes the code knows the schema of the Json data...
    It would be useful to add this to this library.

    Meanwhile, you have to catch the exceptions and act accordingly:

    void setup()
    {
      JSONObject json1 = JSONObject.parse("{\"x\":\"foo\"}");
      JSONObject json2 = JSONObject.parse("{\"x\":{\"val\":\"foo\"}}");
      println(getValue(json1, "x", "val"));
      println(getValue(json2, "x", "val"));
      exit();
    }
    
    String getValue(JSONObject json, String key1, String key2)
    {
      String value = null;
      try
      {
        value = json.getString(key1);
      }
      catch (RuntimeException e)
      {
        JSONObject second = json.getJSONObject(key1); // Assumes it is OK!
        value = second.getString(key2); // Idem...
      } 
      return value;
    }
    

    Implementation is ad hoc, not robust to other cases...

  • Thanks a lot for your help! It works now :)

Sign In or Register to comment.