Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1566798
  • 博文数量: 113
  • 博客积分: 3526
  • 博客等级: 中校
  • 技术积分: 1815
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-08 09:46
个人简介

记录总结自己的工作

文章分类

全部博文(113)

文章存档

2015年(19)

2014年(10)

2013年(6)

2012年(16)

2011年(24)

2010年(21)

2009年(17)

分类: Java

2013-05-17 10:54:51

原文地址:java 解析json格式数据 作者:txgc_wm

       有时候可能会用到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.     }

阅读(1830) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~