如何从一个JSON响应中分割两个对象的数据?

时间:2014-10-28 06:53:03

标签: android json servlets gson

来自服务器的

获得json响应...但是它包含两个对象的数据..一个是ArrayList Type,第二个是一个POJO(HomeVO)类。我想分割数据并存储到不同的对象。我是美国GSON api。

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

2 个答案:

答案 0 :(得分:1)

您有两种选择: 1.手动解析字符串(不推荐) 2.使用Gson将JSon对象转换为对象,然后使用Gson将其转换回一个json对象。

如果您需要更详细的信息,请告诉我

更多探讨:

假设你有两个不同的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!

答案 1 :(得分:0)

我相信你有两个POJO课程用于提问和HomeVO。然后按照以下步骤操作:

您可以使用两个列表(问题和homeVo)创建另一个DTO。



public class ResultDTO {

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

  //use your getters and setters here: 

}
&#13;
&#13;
&#13;

现在,使用这些设置器设置您已经完成的值。

然后将该对象(ResultDTO)传递给你的gson:

&#13;
&#13;
//assuming the ResultDTO object name is resultDto...

response.getWriter().write(new Gson().toJson(resultDto));
&#13;
&#13;
&#13;

现在如果你在客户端检查结果,你可能会得到如下的json响应:

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

所以你可以让你的json对象响应分为这样(这是针对web的,我不知道你如何访问它):

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

试着看看......只是一个指导......没有尝试过......