Json字符串与Object对象相互转换的几种方式
2021-03-14 02:38
标签:setname ide new string except tle 属性 return java 只列举了最省事的方式。不涉及复制情况和速度。 测试用例,一个User类,属性name,age,location。重写toString()。 public class User { 1、Json-Lib 2、org.json 3、Jackson 4、Gson 5、FastJson json-lib时间有些久远,jar包只更新到2010年 org.json用起来有些繁琐 Jackson、Gson、FastJson只需一两句话就可以搞定 来源:迅闻网 Json字符串与Object对象相互转换的几种方式 标签:setname ide new string except tle 属性 return java 原文地址:https://www.cnblogs.com/1156184981651a/p/12813712.htmlJson-Lib、Org.Json、Jackson、Gson、FastJson五种方式转换json类型
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 + ‘\‘‘ +
‘}‘;
}
}
maven依赖如下,需注意
json-lib
测试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 = jsonObject.toString();
System.out.println(json);
//json字符串转成对象
JSONObject jsonObject1 = JSONObject.fromObject(json);
User user1 = (User) JSONObject.toBean(jsonObject1,User.class);
System.out.println(user1);
}
}
maven依赖如下
json
测试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 = jsonObject.getString("name");
Integer age = jsonObject.getInt("age");
String location = jsonObject.getString("location");
User user1 = new User(name,age,location);
System.out.println(user1);
}
}
maven依赖
jackson-databind
测试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 = mapper.readValue(json,User.class);
System.out.println(user1);
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
}
maven依赖
gson
测试demo
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);
}
}
maven依赖
fastjson
测试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.toJSON(user).toString();
System.out.println(json);
//json字符串转成对象
User user1 = JSON.parseObject(json,User.class);
System.out.println(user1);
}
}
上一篇:什么是NodeJs
下一篇:CSC编译和运行C#程序
文章标题:Json字符串与Object对象相互转换的几种方式
文章链接:http://soscw.com/index.php/essay/64391.html