-2
{Response:  responseCode: 200, graphObject: {"id":"108303252910226","first_name":"Chris","last_name":"Jack","email":"adaptech123@gmail.com","gender":"male","birthday":"08\/08\/1984","hometown":{"id":"106377336067638","name":"Bangalore, India"}}, error: null}

I'm getting this response from facebook. how to get the first name

I have tired

 try {
                                String res = response+"";
                                JSONObject j = new JSONObject(res);
                                String graphResponse = j.optString("graphObject");

                                JSONObject j2 = new JSONObject(graphResponse);

                                String first_name = j2.getString("first_name");

                                Log.i("firstname", ""+first_name);
                            }
                            catch (JSONException e)
                            {
                                e.printStackTrace();
                            }
Aravinda
  • 1
  • 1

2 Answers2

0

inside your GraphRequest method

GraphRequest request = GraphRequest.newMeRequest(
                                loginResult.getAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
    public void onCompleted(JSONObject object,GraphResponse response) {
         object.getString("first_name")
    }
});
Nilesh Deokar
  • 2,975
  • 30
  • 53
0

It's multidimensional array so you first spilt that array.

String res="["+"{responseCode: 200, graphObject: {"id":"108303252910226","first_name":"Chris","last_name":"Jack","email":"adaptech123@gmail.com","gender":"male","birthday":"08\/08\/1984","hometown":{"id":"106377336067638","name":"Bangalore, India"}}, error: null}"+"]";
            JSONObject c;
            JSONArray data=null;
            try {
                data = new JSONArray(res);
            } catch (JSONException e1) {
                e1.printStackTrace();
            }  
      try {
         if(data != null) {
               for (int i = 0; i < data.length(); i++) {
                  c = data.getJSONObject(i);
                  String lat=c.getString("responseCode");
                   JSONArray dd=      c.getJSONArray("graphObject")
                      }

then json encode with dd string.

Jay Rathod
  • 11,131
  • 6
  • 34
  • 58