我的应用程序做什么: 我已经为我的网站实现了 facebook 登录。我想从 facebook graph api 中保存一些用户信息,我正在通过 Json 获得响应。
我的问题: 我能够从 json 响应中打印一些单独的值,但无法打印嵌套在数组中的值。如何从 json 响应中打印子值?
*Please see my code below. There i am able to print users info like name and id.
But unable to get the location of user from json response.*
json输出:
--responseBody---{"id":"100003645579131","name":"Ashutosh Singh","first_name":"Ashutosh","location":{"id":"106377336067638","name":"Bangalore, India"}}
---json---{"work":[{"position":{"id":"140001066034322","name":"Senior Inspector"},"location":{"id":"106377336067638","name":"Bangalore, India"},"name":"Ashutosh Singh","last_name":"Singh","gender":"male"}
我打印上述值的代码:
String newUrl = "https://graph.facebook.com/me?access_token=" + accessToken;
httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(newUrl);
System.out.println("Get info from facebook --> executing request: "+ httpget.getURI());
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
System.out.println("--responseBody---"+responseBody);
json = (JSONObject) JSONSerializer.toJSON(responseBody);
System.out.println("---json---"+json);
facebookId = json.getString("id");//this values are printing fine
String firstName = json.getString("first_name");//this values are printing fine
String lastName = json.getString("last_name");//this values are printing fine
String location=json.getJSONArray("location").toString(); //Unable to print information. Plz give help me. I want to print the location name from the json results.
System.out.println("locale:"+location); //Unable to print
错误信息:
javax.servlet.ServletException: net.sf.json.JSONException: JSONObject["location"] is not a JSONArray.
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:520)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:427)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
net.sf.json.JSONException: JSONObject["location"] is not a JSONArray.
net.sf.json.JSONObject.getJSONArray(JSONObject.java:1793)
com.ebhasin.bstalentscareers.action.SecurityServlet.getUserMailAddressFromJsonResponse(SecurityServlet.java:88)
com.ebhasin.bstalentscareers.action.FacebookLoginAction.execute(FacebookLoginAction.java:45)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)