java字符串转换json对象_Java实现Json字符串与Object对象
相互转换的⽅式总结
本⽂实例总结了Java实现Json字符串与Object对象相互转换的⽅式。分享给⼤家供⼤家参考,具体如下:
Json-Lib、Org.Json、Jackson、Gson、FastJson五种⽅式转换json类型
只列举了最省事的⽅式。不涉及复制情况和速度。
测试⽤例,⼀个User类,属性name,age,location。重写toString()。
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 + '\'' +
'}';
}
json转换对象}
1、Json-Lib
maven依赖如下,需注意jdk15,jar包区分jdk1.3和jdk1.5版本net.sf.json-lib
json-lib
2.4
jdk15
测试demo
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 = String();
System.out.println(json);
/
/json字符串转成对象
JSONObject jsonObject1 = JSONObject.fromObject(json);
User user1 = (User) Bean(jsonObject1,User.class); System.out.println(user1);
}
}
2、org.json
maven依赖如下
org.json
json
20170516
测试demo
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 = String("name");
Integer age = Int("age");
String location = String("location");
User user1 = new User(name,age,location);
System.out.println(user1);
}
}
3、Jackson
maven依赖
com.
jackson-databind
2.9.0
测试demo
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 = adValue(json,User.class); System.out.println(user1);
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
}
4、Gson
maven依赖
gson
2.8.1
测试demo
le.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 = Json(user);
System.out.println(json);
//json字符串转成对象
User user1 = gson.fromJson(json,User.class);
System.out.println(user1);
}
}
5、FastJson
maven依赖
com.alibaba
fastjson
1.2.37
测试demo
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(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在线⼯具供⼤家参考使⽤:希望本⽂所述对⼤家java程序设计有所帮助。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。