2014-10-28 67 views
0

從服務器我收到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. "); 
     } 
+0

您的JSON是無效 – thepoosh 2014-10-28 07:35:08

+0

你最好先改變你的servlet的一面。您沒有返回有效的json。你並肩連接兩個jsons。 – Devrim 2014-10-28 10:17:48

回答

1

你有兩個選擇: 1.手動解析字符串(不推薦什麼) 2.轉換的JSON對象爲使用GSON對象,然後將其轉換回一個json對象也使用Gson。

讓我知道,如果你需要更詳細的信息

更多EXPL:

比方說u有兩種不同的JSON字符串,稱爲JsonA和JSonB。爲了加入他們 ,你必須下載GSON庫

class AClass{ 
int idA; 
String nameA; 
} // Note that the variable's names must be the same as the identifiers in JSON 

class BClass{ 
int idB; 
String nameB; 
} 
class JoinedClass{ 
BClass bClass; 
AClass aClass; //don't forget getters and setters 
} 
public String joinJson(String JsonA , String JsonB){ 
Gson gson = new Gson(); 
AClass aClass = new AClass(); 
BClass bClass = new BClass(); 

aClass = gson.fromJson(JsonA, AClass.class); 
bClass = gson.fromJson(JsonB, BClass.class); 
JoinedClass joinedClass = new JoinedClass(); 
joinedClass.setAClass(aClass); 
joinedClass.setBClass(bClass); 
return gson.toJson(joinedClass); 
} 
// but you know, just after writing this code, i found that there might be an easier way to do this. 
// Thanks for attention! 
+0

是的請詳細說明你的答案... thnx – Deep 2014-10-28 09:20:01

0

我相信你有兩個POJO類的問題和HomeVO。然後按照下列步驟操作:

您可以使用兩個列表(問題和homeVo)創建另一個DTO。

public class ResultDTO { 
 

 
    private List <HomeVO> homeVoList; 
 
    private List <Question> questionList; 
 

 
    //use your getters and setters here: 
 

 
}

現在,使用這些制定者像你已經做了設置你的價值觀。

然後將該對象(ResultDTO)傳遞給您的GSON:

//assuming the ResultDTO object name is resultDto... 
 

 
response.getWriter().write(new Gson().toJson(resultDto));

現在,如果你檢查結果在客戶端,你可能有類似下面的JSON響應:

[questionList: [{ 
 
    "questionId": 2, 
 
    "question": "Quality", 
 
    "typeOfQuestion": 2 
 
}, {...}, ], 
 
homeVoList: [{ 
 
    "feedbackName": "IMS", 
 
    "expiryDate": "2014-12-12", 
 
    "createdDate": "2014-10-24", 
 
    "feedbackId": 2 
 
}, {..}]

這樣你就可以得到響應分爲這樣的(這是網絡,我不知道你是怎麼訪問)您的JSON對象:

//assuming the json reponse is 'data': 
 
var homeVoList = data.homeVoList; 
 
var questionList = data.questionList;

試一試,看看...只是指導......還沒有真正嘗試..

相關問題