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.     
  26.     private String message;
  27.     private List<SurveyVO> result;
  28.     public String getMessage() {
  29.         return message;
  30.     }
  31.     public void setMessage(String message) {
  32.         this.message = message;
  33.     }
  34.     public List<SurveyVO> getResult() {
  35.         return result;
  36.     }
  37.     public void setResult(List<SurveyVO> result) {
  38.         this.result = result;
  39.     }    
  40. }

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.     }
原文地址:https://www.cnblogs.com/likeju/p/5032697.html