java 解析json格式数据(转)

 

2012-07-30 16:43:54|  分类: java |  标签:java  json  |举报|字号 订阅

 
 
有时候我们可能会用到json格式的数据进行数据的传输,那么我们怎么把接收到的数据解析出来呢? 
  下面有两种方法: 
1、通过谷歌的Gson来进行解析: 
json数据:sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{surveyid":"2","surveyname":"C"}]}; 

两个VO类: 
Java代码  收藏代码
  1. public class SurveyVO {  
  2.   
  3.     private String surveyId;  
  4.     private String surveyName;  
  5.     private String message;  
  6.   
  7.     public String getMessage() {  
  8.         return message;  
  9.     }  
  10.   
  11.     public void setMessage(String message) {  
  12.         this.message = message;  
  13.     }  
  14.   
  15.     public String getSurveyId() {  
  16.         return surveyId;  
  17.     }  
  18.   
  19.     public void setSurveyId(String surveyId) {  
  20.         this.surveyId = surveyId;  
  21.     }  
  22.   
  23.     public String getSurveyName() {  
  24.         return surveyName;  
  25.     }  
  26.   
  27.     public void setSurveyName(String surveyName) {  
  28.         this.surveyName = surveyName;  
  29.     }  
  30.   
  31. }  
  32.   
  33. public class SurveyListVO {  
  34.       
  35.     private String message;  
  36.     private List<SurveyVO> result;  
  37.     public String getMessage() {  
  38.         return message;  
  39.     }  
  40.     public void setMessage(String message) {  
  41.         this.message = message;  
  42.     }  
  43.     public List<SurveyVO> getResult() {  
  44.         return result;  
  45.     }  
  46.     public void setResult(List<SurveyVO> result) {  
  47.         this.result = result;  
  48.     }  
  49.       
  50.       
  51. }  


json格式转换成object类型: 
Java代码  收藏代码
  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());  print: 1////2  
  9.                     System.out.print(surveyListVO.getResult().get(i)  
  10.                             .getSurveyName());   print: B///C  
  11.                     System.out.print(surveyListVO.getMessage());   print: success  
  12.             }  
  13.         }  


2、通过json-org.jar包进行解析: 

json数据:sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{surveyid":"2","surveyname":"C"}]}; 

一个VO类: 
Java代码  收藏代码
  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类型: 
Java代码  收藏代码
  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.         }  
posted @ 2015-01-04 15:31  u0mo5  阅读(176)  评论(0编辑  收藏  举报