详谈JSON与Javabean转换的几种形式
程序员文章站
2024-02-24 17:22:34
json格式的数据传递是最常用的方法之一,以下列出了常用的几种形态以及与javabean之间的转换:
string json1="{'name':'zhangsa...
json格式的数据传递是最常用的方法之一,以下列出了常用的几种形态以及与javabean之间的转换:
string json1="{'name':'zhangsan','age':23,'interests':[{'interest':'篮球','colors':['绿色','黄色']},{'interest':'足球','colors':['红色','蓝色']}]}"; string json2="[{'name':'zhangsan'},{'name':'lisi'},{'name':'王五'}]"; string json3="{'1':{'name':'zhangsan'},'3':{'name':'lisi'},'4':{'name':'wangwu'}}";//map string json4="{'name':'zhangsan','age':23}";
首先,此处的转化依赖两个jar包
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupid>com.google.code.gson</groupid> <artifactid>gson</artifactid> <version>2.8.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.json/json --> <dependency> <groupid>org.json</groupid> <artifactid>json</artifactid> <version>20170516</version> </dependency>
其次,封装的javabean代码如下
import java.util.list; public class userbean { private string name; private integer age; private list<interestbean> interests; public string getname() { return name; } public void setname(string name) { this.name = name; } public integer getage() { return age; } public void setage(integer age) { this.age = age; } public list<interestbean> getinterests() { return interests; } public void setinterests(list<interestbean> interests) { this.interests = interests; } class interestbean{ private string interest; private list<string> colors; public string getinterest() { return interest; } public void setinterest(string interest) { this.interest = interest; } public list<string> getcolors() { return colors; } public void setcolors(list<string> colors) { this.colors = colors; } } }
1、普通的json4格式的json解析:
public void testparsejson(){ jsonobject jsonobj = new jsonobject(json4); string name = jsonobj.getstring("name"); int age = jsonobj.getint("age"); system.out.println(name); system.out.println(age); userbean user = new userbean(); user.setage(age); user.setname(name); }
2、数组形式的json解析以及gson解析:
public void testjsonarray(){ jsonarray jsonarray = new jsonarray(json2); for (int i = 0; i < jsonarray.length(); i++) { jsonobject jsonobj = jsonarray.getjsonobject(i); string name = jsonobj.getstring("name"); system.out.println(name); } }
/** * 解析json数组 */ public void testparselistjson(){ gson gson = new gson(); type type = new typetoken<list<userbean>>(){}.gettype(); list<userbean> users = gson.fromjson(json2, type); for(userbean user:users){ system.out.println(user.getname()); } }
3、内嵌json形式的json与gson解析:
/** * 内嵌json解析 */ public void testparsejson1(){ jsonobject rootjson = new jsonobject(json1); jsonarray jsoninterestarray = rootjson.getjsonarray("interests"); for (int i = 0; i < jsoninterestarray.length(); i++) { jsonobject interestjsonobj = jsoninterestarray.getjsonobject(i); string interest = interestjsonobj.getstring("interest"); system.out.println(interest); object obj = interestjsonobj.get("colors"); system.out.println(obj); } }
/** * 内嵌gson解析 */ public void testsimplejson(){ gson gson = new gson(); userbean user = gson.fromjson(json1, userbean.class); system.out.println(user.getname()); system.out.println(user.getage()); system.out.println(user.getinterests().size()); list<interestbean> list = user.getinterests(); for(interestbean bean:list) { system.out.println(bean.getinterest()); list<string> colors = bean.getcolors(); for(string color:colors){ system.out.println(color); } } }
4、map形式的json的gson解析:
/** * 解析一个map类型的json */ public void testparsemapjson(){ gson gson = new gson(); type type = new typetoken<map<string,userbean>>(){}.gettype(); map<string,userbean> map = gson.fromjson(json3, type); set<string> keys = map.keyset(); for(string key:keys){ userbean bean = map.get(key); system.out.println(key); system.out.println(bean.getname()); } }
5、将一个javabean对象封装成json格式
/** * 将一个javabean对象封装成json格式 */ public string testjavabean2json(){ userbean userbean = new userbean(); userbean.setname("zhangsan"); userbean.setage(33); list<interestbean> list = new arraylist<interestbean>(); interestbean bean1 = new userbean().new interestbean(); bean1.setinterest("篮球1"); interestbean bean2 = new userbean().new interestbean(); bean2.setinterest("篮球2"); list.add(bean1); list.add(bean2); userbean.setinterests(list); //将user bean转换成json gson gson = new gson(); string jsonstr = gson.tojson(userbean); system.out.println(jsonstr); return jsonstr; } }
仅供参考,如有雷同,纯属巧合^_^
以上这篇详谈json与javabean转换的几种形式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。