1

I am new to JSON. I'm working with the json processing library. Given a JSON such as:

{
   "data": [
      {
         "name": "John Doe",
         "id": "2980311"
      },
      {
         "name": "Jane Doe",
         "id": "10221412"
      },
      {
         "name": "George Doe",
         "id": "111623489"
      }
    ],
    "paging": {
      "blah" : "blah"
    }
}

How can I get the id values. I've started by trying to get an array to work with:

String URL = "https://graph.facebook.com/username/friends?access_token=";
String token = "";
String[] response = loadStrings(URL+token);

if(response != null) {
  JSONObject result = new JSONObject(response);
  JSONArray data = result.getJSONArray("data");
}

And this prints out:

JSONObject["data"] not found.
JSONObject["data"] is not a JSONArray.

But it's definitely getting the data. If I print out response I see the data.

What am I missing?

2 Answers 2

2

I'm not seeing a JSONObject constructor in that library that accepts String[] (the type of your response variable). There's JSONObject(String) and there's JSONObject(Object). If you pass a String[] into JSONObject, it'll match the latter, which doesn't (immediately) look to me like it'll do what you want.

I suspect you want to get a single String from the URL, which you'd then pass into JSONObject(String), at which point things should start working correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

@mix: Excellent! It was a bit of a shot in the dark as I've not used that library, glad that helped!
0

try with this..

GraphObject responseGraphObject = response.getGraphObject();
JSONObject json = responseGraphObject.getInnerJSONObject();

                      JSONObject sys  = json.getJSONObject("data");
          Log.e("urlimage json",  sys.getString("url"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.