JAVA使用Gson解析json数据实例解析
JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成。同XML一样是一种“传输格式”。JSON采用与编程语言无关的文本格式,便于数据传输、存储、交换。
封装类Attribute:
publicclassAttribute{ privateintid; privateStringname; privateintage; publicintgetId(){ returnid; } publicvoidsetId(intid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } }
另一个封装类GsonDataAnalysis:
publicclassGsonDataAnalysis{ privateList<Attribute>list; publicList<Attribute>getList(){ returnlist; } publicvoidsetList(List<Attribute>list){ this.list=list; } }
测试类Main方法:
/** *Gson解析json数据 *@authorForeverLover *@version2015-04-19 */ publicclassGsonTest{ publicstaticvoidmain(String[]args){ StringsTotalString="{\"list\":["+ "{\"id\":\"1\",\"name\":\"Tom\",\"age\":\"12\"},"+ "{\"id\":\"2\",\"name\":\"Marry\",\"age\":\"18\"}"+ "]}"; Gsongson=newGson(); GsonDataAnalysisgda=gson.fromJson( sTotalString,GsonDataAnalysis.class); for(inti=0;i<gda.getList().size();i++){ System.out.print("ID号:"+gda.getList().get(i).getId()+""); System.out.print("姓名:"+gda.getList().get(i).getName()+""); System.out.println("年龄:"+gda.getList().get(i).getAge()); } } }
注:在使用Gson类的时候在工程中要添加Gson.jar包。
下面给大家介绍java解析json格式数据
有时候可能会用到json格式进行数据的传输,那么怎么把接收到的数据解析出来呢?下面介绍两种解析json数据的方法:
1、通过谷歌的Gson来进行解析:
json数据:sTotalString={"message":"success","result":[{"surveyid":"1","surveyname":"B"}{surveyid":"2","surveyname":"C"}]};
两个VO类:
publicclassSurveyVO{ privateStringsurveyId; privateStringsurveyName; privateStringmessage; publicStringgetMessage(){ returnmessage; } publicvoidsetMessage(Stringmessage){ this.message=message; } publicStringgetSurveyId(){ returnsurveyId; } publicvoidsetSurveyId(StringsurveyId){ this.surveyId=surveyId; } publicStringgetSurveyName(){ returnsurveyName; } publicvoidsetSurveyName(StringsurveyName){ this.surveyName=surveyName; } } publicclassSurveyListVO{ privateStringmessage; privateList<SurveyVO>result; publicStringgetMessage(){ returnmessage; } publicvoidsetMessage(Stringmessage){ this.message=message; } publicList<SurveyVO>getResult(){ returnresult; } publicvoidsetResult(List<SurveyVO>result){ this.result=result; } }
json格式转换成object类型:
publicclassfromGson(){ sTotalString={"message":"success","result":[{"surveyid":"1","surveyname":"B"}{"surveyid":"2","surveyname":"C"}]}; Gsongson=newGson(); SurveyListVOsurveyListVO=gson.fromJson(sTotalString, SurveyListVO.class); for(inti=0;i<surveyListVO.getResult().size();i++){ System.out.print(surveyListVO.getResult().get(i) .getSurveyId()); print:1////2 System.out.print(surveyListVO.getResult().get(i) .getSurveyName()); print:B///C System.out.print(surveyListVO.getMessage()); } }
2、通过json-org.jar包进行解析:
json数据:sTotalString={"message":"success","result":[{"surveyid":"1","surveyname":"B"}{surveyid":"2","surveyname":"C"}]};
一个VO类:
publicclassSurveyVO{ privateStringsurveyId; privateStringsurveyName; publicStringgetSurveyId(){ returnsurveyId; } publicvoidsetSurveyId(StringsurveyId){ this.surveyId=surveyId; } publicStringgetSurveyName(){ returnsurveyName; } publicvoidsetSurveyName(StringsurveyName){ this.surveyName=surveyName; } }
json格式转换成object类型:
publicclassfromJson(){ sTotalString={"message":"success","result":[{"surveyid":"1","surveyname":"B"}{"surveyid":"2","surveyname":"C"}]}; JSONObjectjson; try{ json=newJSONObject(sTotalString); JSONArrayresults=json.getJSONArray("result"); for(inti=0;i<results.length();i++){ SurveyVOsurveyVO=newSurveyVO(); JSONObjectresult=results.getJSONObject(i); System.out.println(result.getString("surveyId")+""+result.getString("surveyName")); surveyVO.setSurveyId(result.getString("surveyId")); surveyVO.setSurveyName(result.getString("surveyName")); surveyVOList.add(surveyVO); } }catch(JSONExceptione){ e.printStackTrace(); } }