<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
包:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
public static void main(String[] args) throws JsonProcessingException {
Map<String,Object> map = new HashMap<>();
map.put("name","zs");
map.put("age",18);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(map);
System.out.println(json); // {"name":"zs","age":18}
}
public static void main(String[] args) throws JsonProcessingException {
Map<String,Object> map = new HashMap<>();
map.put("name","zs");
map.put("age",18);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(map);
System.out.println(json); // {"name":"zs","age":18}
// 将字符串转为 map
Map<String,Object> map1 = mapper.readValue(json, Map.class);
System.out.println(map1.get("name")); // zs
}