JAVA使用Gson解析json数据实例解析
程序员文章站
2024-03-08 22:06:04
json(javascript object notation) 是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成。同xml一样是一种“传输格式”。js...
json(javascript object notation) 是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成。同xml一样是一种“传输格式”。json采用与编程语言无关的文本格式,便于数据传输、存储、交换。
封装类attribute:
public class attribute { private int id; private string name; private int age; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public int getage() { return age; } public void setage(int age) { this.age = age; } }
另一个封装类gsondataanalysis:
public class gsondataanalysis { private list<attribute> list; public list<attribute> getlist() { return list; } public void setlist(list<attribute> list) { this.list = list; } }
测试类main方法:
/** * gson解析json数据 * @author foreverlover * @version 2015-04-19 */ public class gsontest { public static void main(string[] args) { string stotalstring = "{\"list\": [" + "{\"id\":\"1\",\"name\":\"tom\",\"age\":\"12\"}," + "{\"id\":\"2\",\"name\":\"marry\",\"age\":\"18\"}" + "]}"; gson gson = new gson(); gsondataanalysis gda = gson.fromjson( stotalstring, gsondataanalysis.class); for(int i = 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类:
public class surveyvo { private string surveyid; private string surveyname; private string message; public string getmessage() { return message; } public void setmessage(string message) { this.message = message; } public string getsurveyid() { return surveyid; } public void setsurveyid(string surveyid) { this.surveyid = surveyid; } public string getsurveyname() { return surveyname; } public void setsurveyname(string surveyname) { this.surveyname = surveyname; } } public class surveylistvo { private string message; private list<surveyvo> result; public string getmessage() { return message; } public void setmessage(string message) { this.message = message; } public list<surveyvo> getresult() { return result; } public void setresult(list<surveyvo> result) { this.result = result; } }
json格式转换成object类型:
public class fromgson() { stotalstring = {"message":"success","result":[{"surveyid":"1","surveyname":"b"}{"surveyid":"2","surveyname":"c"}]}; gson gson = new gson(); surveylistvo surveylistvo = gson.fromjson(stotalstring, surveylistvo.class); for (int i = 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类:
public class surveyvo { private string surveyid; private string surveyname; public string getsurveyid() { return surveyid; } public void setsurveyid(string surveyid) { this.surveyid = surveyid; } public string getsurveyname() { return surveyname; } public void setsurveyname(string surveyname) { this.surveyname = surveyname; } }
json格式转换成object类型:
public class fromjson() { stotalstring = {"message":"success","result":[{"surveyid":"1","surveyname":"b"}{"surveyid":"2","surveyname":"c"}]}; jsonobject json; try { json = new jsonobject(stotalstring); jsonarray results = json.getjsonarray("result"); for (int i = 0; i < results.length(); i++) { surveyvo surveyvo = new surveyvo(); jsonobject result = 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 (jsonexception e) { e.printstacktrace(); } }
上一篇: java设计优化之代理模式