jackson完成json和对象/map/list互转
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
jackson是一款非常好用的json转换工具,总结一下具体用法
一:导入依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.3</version>
</dependency>
二:User
package com.mote.jackson;
public class User {
private String name;
private Integer age;
private String city;
public User() {
super();
}
public User(String name, Integer age, String city) {
super();
this.name = name;
this.age = age;
this.city = city;
}
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 getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", city=" + city + "]";
}
}
三:jackson操作
package com.mote.jackson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JackSonDemo {
private static ObjectMapper MAPPER = new ObjectMapper();
public static void main(String[] args) throws Exception {
//对象转json
User user = new User("tom",23,"上海");
String json = MAPPER.writeValueAsString(user);
System.out.println(json);
//json转对象
User uuser = MAPPER.readValue(json, User.class);
System.out.println(uuser);
//map<String,String>转json
HashMap<String, String> map = new HashMap<String,String>();
map.put("name", "jack");map.put("city", "beijin");
String mjson = MAPPER.writeValueAsString(map);
System.out.println(mjson);
//json转map<String,String>
HashMap<String,String> mmap = MAPPER.readValue(mjson, HashMap.class);
System.out.println(mmap);
//map<String,User>转json
HashMap<String, User> umap = new HashMap<String, User>();
umap.put(user.getName(), user);
String mmjson = MAPPER.writeValueAsString(umap);
System.out.println(mmjson);
//json转map<String,User>
JavaType jvt = MAPPER.getTypeFactory().constructParametricType(HashMap.class,String.class,User.class);
Map<String,User> urMap = MAPPER.readValue(mmjson, jvt);
System.out.println(urMap);
//list<String>转json
ArrayList<String> list = new ArrayList<String>();
list.add("jack");
list.add("tom");
String ljson = MAPPER.writeValueAsString(list);
System.out.println(ljson);
//json转list<String>
ArrayList<String> sList = MAPPER.readValue(ljson, ArrayList.class);
System.out.println(sList);
//list<User>转json
List<User> ulist = new ArrayList<User>();
ulist.add(user);
String ujson = MAPPER.writeValueAsString(ulist);
System.out.println(ujson);
//json转list<User>
JavaType jt = MAPPER.getTypeFactory().constructParametricType(ArrayList.class, User.class);
List<User> urlist = MAPPER.readValue(ujson, jt);
System.out.println(urlist);
}
}
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e
3 个月前
8c391e04
6 个月前
更多推荐
已为社区贡献2条内容
所有评论(0)