1. 程式人生 > >java解析json格式資料

java解析json格式資料

有時候可能會用到json格式進行資料的傳輸,那麼怎麼把接收到的資料解析出來呢? 下面介紹兩種解析json資料的方法:

       1、通過谷歌的Gson來進行解析: 
json資料:sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{surveyid":"2","surveyname":"C"}]}; 

兩個VO類: 
  1. public class SurveyVO {
  2.     private String surveyId;
  3.     private String surveyName;
  4.     private
     String message;
  5.     public String getMessage() {
  6.         return message;
  7.     }
  8.     public void setMessage(String message) {
  9.         this.message = message;
  10.     }
  11.     public String getSurveyId() {
  12.         return surveyId;
  13.     }
  14.     public void setSurveyId(String surveyId) {
  15.         this.surveyId = surveyId;

  16.     }
  17.     public String getSurveyName() {
  18.         return surveyName;
  19.     }
  20.     public void setSurveyName(String surveyName) {
  21.         this.surveyName = surveyName;
  22.     }
  23. }
  24. public class SurveyListVO {
  25.     private String message;
  26.     private List<SurveyVO> result;
  27.     public String getMessage()
     {
  28.         return message;
  29.     }
  30.     public void setMessage(String message) {
  31.         this.message = message;
  32.     }
  33.     public List<SurveyVO> getResult() {
  34.         return result;
  35.     }
  36.     public void setResult(List<SurveyVO> result) {
  37.         this.result = result;
  38.     }    
  39. }
json格式轉換成object型別:
  1. public class fromGson() {
  2.         sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{"surveyid":"2","surveyname":"C"}]};
  3.         Gson gson = new Gson();
  4.         SurveyListVO surveyListVO = gson.fromJson(sTotalString,
  5.                                     SurveyListVO.class);
  6.         for (int i = 0; i < surveyListVO.getResult().size(); i++) {
  7.             System.out.print(surveyListVO.getResult().get(i)
  8.                              .getSurveyId());
  9.             print: 1////2
  10.             System.out.print(surveyListVO.getResult().get(i)
  11.                              .getSurveyName());
  12.             print: B///C
  13.             System.out.print(surveyListVO.getMessage());
  14.         }
  15.     }


      2、通過json-org.jar包進行解析: 
json資料:sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{surveyid":"2","surveyname":"C"}]}; 

一個VO類: 
  1. public class SurveyVO {
  2.     private String surveyId;
  3.     private String surveyName;
  4.     public String getSurveyId() {
  5.         return surveyId;
  6.     }
  7.     public void setSurveyId(String surveyId) {
  8.         this.surveyId = surveyId;
  9.     }
  10.     public String getSurveyName() {
  11.         return surveyName;
  12.     }
  13.     public void setSurveyName(String surveyName) {
  14.         this.surveyName = surveyName;
  15.     }
  16. }
json格式轉換成object型別: 

  1. public class fromJson() {
  2.         sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{"surveyid":"2","surveyname":"C"}]};
  3.         JSONObject json;
  4.         try {
  5.             json = new JSONObject(sTotalString);
  6.             JSONArray results = json.getJSONArray("result");
  7.             for (int i = 0; i < results.length(); i++) {
  8.                 SurveyVO surveyVO = new SurveyVO();
  9.                 JSONObject result = results.getJSONObject(i);
  10.                 System.out.println(result.getString("surveyId")+" "+result.getString("surveyName"));
  11.                 surveyVO.setSurveyId(result.getString("surveyId"));
  12.                 surveyVO.setSurveyName(result.getString("surveyName"));
  13.                 surveyVOList.add(surveyVO);
  14.             }
  15.         } catch (JSONException e) {
  16.             e.printStackTrace();
  17.         }
  18.     }