本文实例总结了java实现json字符串与object对象相互转换的方式。分享给大家供大家参考,具体如下:
json-lib、org.json、jackson、gson、fastjson五种方式转换json类型
只列举了最省事的方式。不涉及复制情况和速度。
测试用例,一个user类,属性name,age,location。重写tostring()。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
public class user { private string name; private integer age; private string location; public user() { } public user(string name) { this .name = name; } public user(string name, integer age) { this .name = name; this .age = age; } public user(string name, integer age, string location) { this .name = name; this .age = age; this .location = location; } 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 string getlocation() { return location; } public void setlocation(string location) { this .location = location; } @override public string tostring() { return "user{" + "name='" + name + '\ '' + ", age=" + age + ", location='" + location + '\ '' + '}' ; } } |
1、json-lib
maven依赖如下,需注意<classifier>jdk15</classifier>,jar包区分jdk1.3和jdk1.5版本
1
2
3
4
5
6
|
<dependency> <groupid>net.sf.json-lib</groupid> <artifactid>json-lib</artifactid> <version> 2.4 </version> <classifier>jdk15</classifier> </dependency> |
测试demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import net.sf.json.jsonobject; public class jsonlibdemo { public static void main(string[] args) { //创建测试object user user = new user( "李宁" , 24 , "北京" ); system.out.println(user); //转成json字符串 jsonobject jsonobject = jsonobject.fromobject(user); string json = jsonobject.tostring(); system.out.println(json); //json字符串转成对象 jsonobject jsonobject1 = jsonobject.fromobject(json); user user1 = (user) jsonobject.tobean(jsonobject1,user. class ); system.out.println(user1); } } |
2、org.json
maven依赖如下
1
2
3
4
5
|
<dependency> <groupid>org.json</groupid> <artifactid>json</artifactid> <version> 20170516 </version> </dependency> |
测试demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import org.json.jsonobject; public class orgjsondemo { public static void main(string[] args) { //创建测试object user user = new user( "李宁" , 24 , "北京" ); system.out.println(user); //转成json字符串 string json = new jsonobject(user).tostring(); system.out.println(json); //json字符串转成对象 jsonobject jsonobject = new jsonobject(json); string name = jsonobject.getstring( "name" ); integer age = jsonobject.getint( "age" ); string location = jsonobject.getstring( "location" ); user user1 = new user(name,age,location); system.out.println(user1); } } |
3、jackson
maven依赖
1
2
3
4
5
|
<dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid> <version> 2.9 . 0 </version> </dependency> |
测试demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import com.fasterxml.jackson.databind.objectmapper; public class jacksondemo { public static void main(string[] args) { //创建测试object user user = new user( "李宁" , 24 , "北京" ); system.out.println(user); //转成json字符串 objectmapper mapper = new objectmapper(); try { string json = mapper.writevalueasstring(user); system.out.println(json); //json字符串转成对象 user user1 = mapper.readvalue(json,user. class ); system.out.println(user1); } catch (java.io.ioexception e) { e.printstacktrace(); } } } |
4、gson
maven依赖
1
2
3
4
5
|
<dependency> <groupid>com.google.code.gson</groupid> <artifactid>gson</artifactid> <version> 2.8 . 1 </version> </dependency> |
测试demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import com.google.gson.gson; public class gsondemo { public static void main(string[] args) { //创建测试object user user = new user( "李宁" , 24 , "北京" ); system.out.println(user); //转成json字符串 gson gson = new gson(); string json = gson.tojson(user); system.out.println(json); //json字符串转成对象 user user1 = gson.fromjson(json,user. class ); system.out.println(user1); } } |
5、fastjson
maven依赖
1
2
3
4
5
|
<dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version> 1.2 . 37 </version> </dependency> |
测试demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import com.alibaba.fastjson.json; public class fastjsondemo { public static void main(string[] args) { //创建测试object user user = new user( "李宁" , 24 , "北京" ); system.out.println(user); //转成json字符串 string json = json.tojson(user).tostring(); system.out.println(json); //json字符串转成对象 user user1 = json.parseobject(json,user. class ); system.out.println(user1); } } |
json-lib时间有些久远,jar包只更新到2010年
org.json用起来有些繁琐
jackson、gson、fastjson只需一两句话就可以搞定
ps:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用:
在线json代码检验、检验、美化、格式化工具:https://tool.zzvips.com/t/jsonjiexi/
json在线格式化工具:https://tool.zzvips.com/t/jsonformat/
在线excel/json互相转换工具:https://tool.zzvips.com/t/excel2json/
json/excel互相转换工具:https://tool.zzvips.com/t/json2excel/
在线json压缩/转义工具:https://tool.zzvips.com/t/jsonzip/
希望本文所述对大家java程序设计有所帮助。
原文链接:https://blog.csdn.net/n447194252/article/details/77747465