JSONObject not a JSONArray

edited April 2015 in Using Processing

Hello I am making an http request to Facebook api I have some problems with the json response.

my code is

 import http.requests.*;
 public void setup() 
 {
   GetRequest get = new GetRequest("graph.facebook.com/525486630/?fields=picture&type=large");
    get.send(); 
    JSONObject ob = parseJSONObject(response);  
    JSONArray arr = ob.getJSONArray("picture");
 }

and the json response is

{
 "picture": {
                 "data": {
                              "is_silhouette": false,
                              "url":  "fbcdn-profile-a.akamaihd.net/hprofile-ak-xfp1/v/t1.0-          1/c14.0.50.50/p50x50/10390401_10152154496321631_5043335340023918760_n.jpg? oh=9623d5bf87b80de1d0bf0b16b983d6ac&oe=55ADC3E6&__gda__=1440938889_bb7443e0b9b9eb3d73434d40f183594 7"
            }
  },
 "id": "525486630"
 }

what I am getting is that JSONObject["picture"] is not a JSONArray. What I want to do is to go deeper and retrieve the url of the image.. what I am doing wrong?

Answers

  • Well, the error says it all: the picture element of that json is not an array, it's an object. In your code: what is ob? What members does it have?

  • ob is the content of the http response which is the JSON file

  • This would be easier if you posted an MCVE. Also, it might be easier for you to work in JavaScript mode if you're dealing with JSON like this.

  • Why this is not MCVE? I do no thinks that not using Javascript is the problem!

  • I'm not saying that the problem is caused by not using JavaScript. I'm just saying that it might be easier to do this in JavaScript, since you could manipulate the JSON directly.

    You're right, this might be an MCVE, but I can't acccess facebook from here. I'll try it when I get home.

  • edited April 2015

    you don't have to access Facebook ! What do you mean? My question is about parsing a json file

  • Have you tried JSONObject picture = ob.getJSONObject("picture");?

  • Cool! this is working but now I am having another problem..

    my new code is

       JSONObject ob = parseJSONObject(response);
    
       JSONObject picture = ob.getJSONObject("picture");
    
       JSONObject data = picture.getJSONObject("data");
       JSONObject url = data.getJSONObject("url");  
    

    but I am getting JSONObject "url" is not a JSONObject!!!

  • Answer ✓

    ok I found it!!

    the last line

    JSONObject url = data.getJSONObject("url");

    Should be

    String url = data.getString("url");

    Case Solved!

  • Answer ✓

    i'm guessing that's because it's not an object, it's a terminal node, or leaf or whatever.

    https://processing.org/reference/JSONObject.html

    try data.getString("url");

  • Because url is a string...

Sign In or Register to comment.