從服務器我收到JSON響應...但它包含兩個對象的數據..一個是ArrayList類型,第二個是一個POJO(HomeVO)類。我想分割數據並存儲到不同的對象中。我是usnig GSON api。如何從一個JSON響應中分離兩個對象的數據?
Servlet:
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(new Gson().toJson(questions));
response.getWriter().write(new Gson().toJson(homeVo));
Json Response:
[{"questionId":2,"question":"Quality","typeOfQuestion":2}, {"questionId":3,"question":"Satisfaction","typeOfQuestion":1},{"questionId":4,"question":"overall","typeOfQuestion":2}]{"feedbackName":"IMS","expiryDate":"2014-12-12","createdDate":"2014-10-24","feedbackId":2}
Android Parsing:
HttpClient httpClient = WebServiceUtils.getHttpClient();
try {
HttpResponse response = httpClient.execute(new HttpGet(url));
HttpEntity entity = response.getEntity();
Reader reader = new InputStreamReader(entity.getContent());
data = gson.fromJson(reader, arrayListType);
} catch (Exception e) {
e.printStackTrace();
Log.i("json array",
"While getting server response server generate error. ");
}
您的JSON是無效 – thepoosh 2014-10-28 07:35:08
你最好先改變你的servlet的一面。您沒有返回有效的json。你並肩連接兩個jsons。 – Devrim 2014-10-28 10:17:48