0

{Response: responseCode: 200, graphObject: {"id":"98374978103","birthday":"04\/19\/1991","gender":"male","email":"email@gmail.com","name":"Wasfasf"}, error: null} I am getting facebook response as above string How to parse this string. I am doing it this way

 try {
                                String res = response.toString();
                                JSONObject obj = new JSONObject(res);
                                JSONArray arr = obj.optJSONArray("graphObject");
                                for(int i=0; i < arr.length(); i++) {
                                    JSONObject jsonObject = arr.getJSONObject(i);
                                    String id = jsonObject.optString("id").toString();
                                    String bday = jsonObject.optString("birthday").toString();
                                    String gender = jsonObject.optString("gender").toString();
                                    String email = jsonObject.optString("email").toString();
                                    String name = jsonObject.optString("name").toString();


                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                            }

It throws the exception :

Unterminated object at character 25 of {Response:  responseCode: 200, graphObject: {"id":"983797084978103","birthday":"04\/19\/1991","gender":"male","email":"waleedbinilyas@gmail.com","name":"Waleed Bin Ilyas"}, error: null}
user3637297
  • 211
  • 2
  • 5
  • This is json response, it shows this it is invalid json. Please give me correct one – sam_k Jul 29 '15 at 12:36
  • possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Glorfindel Jul 29 '15 at 12:41
  • possible duplicate of [How to parse JSON in Android](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) – Anirudh Sharma Jul 29 '15 at 12:41
  • Still Json is not proper, anyways try above posts which are possible duplicates of your question. If you will not find any solution then post your question with some code what you have tried – sam_k Jul 29 '15 at 12:44
  • You can visit this site http://www.tutorialspoint.com/android/android_json_parser.htm – Ajinkya S Jul 29 '15 at 12:50
  • @sam_k it is the response i am getting from faceboook graph api .. – user3637297 Jul 29 '15 at 12:57
  • Whats problem in your code. Please tell us your problem What you want to do with json response – sam_k Jul 29 '15 at 13:43

6 Answers6

1

I have done in this way and works fine perfectly

public class SignIn extends AppCompatActivity {

    CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //before set conteview
        FacebookSdk.sdkInitialize(getApplicationContext());
      //  AppEventsLogger.activateApp(this);
        callbackManager = CallbackManager.Factory.create();
        setContentView(R.layout.activity_signin);

         LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);

        loginButton.setReadPermissions(Arrays.asList("public_profile", "email"));

        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

                GraphRequest graphRequest=GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {

                        Log.d("Graph Response",graphResponse.toString());

                        String myCustomizedResponse = graphResponse.getJSONObject().toString();

                        Log.d("Ketan_Ramani",graphResponse.getJSONObject().toString());

                        try {
                            JSONObject obj = new JSONObject(myCustomizedResponse);

                            String id = obj.getString("id");
                            String first_name = obj.getString("first_name");
                            String last_name = obj.getString("last_name");
                            String email = obj.getString("email");


                            Log.d("Id",id);
                            Log.d("FirstName",first_name);
                            Log.d("LastName",last_name);
                            Log.d("Email",email);

                        } catch (JSONException e) {
                            Utils.hide_dialog();

                            e.printStackTrace();
                        }


                    }

                });

                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,first_name,last_name,email");
                graphRequest.setParameters(parameters);
                graphRequest.executeAsync();
            }

            @Override
            public void onCancel() {
                // App code
            }

            @Override
            public void onError(FacebookException exception) {
                // App code
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

}
Werner Henze
  • 16,404
  • 12
  • 44
  • 69
Ketan Ramani
  • 4,874
  • 37
  • 42
0

If you want get a object like graphObject do this:

JSONObject jObj = new JSONObject([jsonString]);
ArrayList<SomeClass> listObj = jObj.getJSONArray("graphObject");

if (listObj.length() != 0){
     for (int i = 0; i < listObj.length(); i++) {
          JSONObject c = listObj.getJSONObject(i);
          c.getString("name");
          c.getString("gender");
           .
           .
           .
     }
} 

Edit: SomeClass = Same Fields that you have in your json´s fields, in this case: id,birthday,gender,email,name.

0

I think this would work for you.

try {
    String res = response.toString();
    JSONObject obj = new JSONObject(res);
    JSONArray arr = obj.getJSONArray("graphObject");
    for(int i=0; i < arr.length(); i++) {
        JSONObject jsonObject = arr.getJSONObject(i);
        String id = jsonObject.getString("id").toString();
        String bday = jsonObject.getString("birthday").toString();
        String gender = jsonObject.getString("gender").toString();
        String email = jsonObject.getString("email").toString();
        String name = jsonObject.getString("name").toString();


    }
}    
catch (Exception e) {
    e.printStackTrace();
}

Note : I have not tested this code.

sam_k
  • 5,983
  • 14
  • 76
  • 110
0

The given json is invalid,you can use jsonLint to validte your json Feed assuming feed below the correct feed

{ "Response": { "responseCode": 200, "graphObject": { "id": "983797088103", "birthday": "04/19/1991", "gender": "male", "email": "email@wamil", "name": "name" }, " error": null } }

If you are using an IDE like intellij/Android studio use the plugin like Dtonator to generate the Gson DTO's,which will look like this

public class FaceBookResponse {

@SerializedName("Response")
public Response Response;

public static class GraphObject {
    @SerializedName("birthday")
    public String birthday;
    @SerializedName("gender")
    public String gender;
    @SerializedName("name")
    public String name;
    @SerializedName("id")
    public String id;
    @SerializedName("email")
    public String email;
}

public static class Response {
    @SerializedName("error")
    public String error;
    @SerializedName("graphObject")
    public GraphObject graphObject;
    @SerializedName("responseCode")
    public int responseCode;
}
}

Now you can simply parse your feed using the below two lines.

private final Gson gson = new Gson();
FaceBookResponse response=mGson.fromJson(json, mClazz);
nvinayshetty
  • 3,187
  • 1
  • 21
  • 32
0

I did it in this way

 try {


                                String id = object.getString("id");

                                String uname = object.getString("name");

                                String emailid = object.getString("email");

                                String gender = object.getString("gender");

                                String bday = object.getString("birthday");


                                } catch (Exception e) {
                                e.printStackTrace();
                            }

Graph object is an object not array.

user3637297
  • 211
  • 2
  • 5
0
public void onCompleted(JSONObject facebookResponseObject, GraphResponse response) {
                                // Application code
                               // Log.v("LoginActivity", response.toString());
                                Log.v("facebook_response", facebookResponseObject.toString());
}}

You are converting response object of type GraphResponse and then doing operations on it.

Instead do this:facebookResponseObject.toString() and then you can easily do this --> Log.e("facebook_response_id",id);

Ravi Yadav
  • 2,296
  • 3
  • 25
  • 32