Android Facebook响应解析

时间:2015-07-29 12:28:57

标签: android json parsing facebook-graph-api

{Response: responseCode: 200, graphObject: {"id":"98374978103","birthday":"04\/19\/1991","gender":"male","email":"email@gmail.com","name":"Wasfasf"}, error: null} 我正在上面的字符串获得Facebook响应 如何解析这个字符串。 我是这样做的

 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();
                            }

抛出异常:

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}

6 个答案:

答案 0 :(得分:1)

我已经用这种方式做得很好并且工作得很好

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);
    }

}

答案 1 :(得分:0)

如果你想获得像graphObject这样的对象:

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");
           .
           .
           .
     }
} 

编辑:SomeClass =您在json字段中的相同字段,在本例中为:id,birthday,gender,email,name。

答案 2 :(得分:0)

我认为这对你有用。

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();
}

注意:我没有测试过这段代码。

答案 3 :(得分:0)

给定的json无效,您可以使用jsonLint来验证您的json Feed 假设饲料低于正确的饲料

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

如果您正在使用像intellij / Android studio这样的IDE,请使用像Dtonator这样的插件来生成Gson DTO,它将如下所示

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;
}
}

现在您可以使用以下两行简单地解析您的Feed。

private final Gson gson = new Gson();
FaceBookResponse response=mGson.fromJson(json, mClazz);

答案 4 :(得分:0)

我是这样做的

 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();
                            }

图形对象是一个对象而不是数组。

答案 5 :(得分:0)

public void onCompleted(JSONObject facebookResponseObject, GraphResponse response) {
                                // Application code
                               // Log.v("LoginActivity", response.toString());
                                Log.v("facebook_response", facebookResponseObject.toString());
}}

您正在转换GraphResponse类型的响应对象,然后对其进行操作。

而是这样做:facebookResponseObject.toString()然后你可以轻松地做到这一点 - &gt; Log.e( “facebook_response_id”,ID);