アプリケーションの機能: Web サイトに Facebook ログインを実装しました。そして、Jsonを介して応答を得ているFacebookグラフAPIからユーザー情報を保存したいと思います。
私の問題: 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)