2014-10-28 73 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;

试一试,看看...只是指导......还没有真正尝试..

相关问题