JSON--解析器Jackson--java转json

    科技2025-07-19  6

    黑马程序员

    JSON–解析器Jackson–java转json

    package cn.itcast.web.json; import cn.itcast.web.domain.User; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test; import java.io.File; import java.io.IOException; import java.util.Date; public class JsonTest { @Test public void test1() throws IOException { User user = new User("张三",12,new Date()); ObjectMapper mapper = new ObjectMapper(); String s = mapper.writeValueAsString(user); mapper.writeValue(new File("F:\\a.txt"),user); System.out.println(s); } } {"name":"张三","age":12,"birthday":1602146360562}

    jar包

    JSON–解析器Jackson–java转json–注解

    package cn.itcast.web.domain; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; import java.text.SimpleDateFormat; import java.util.Date; public class User { private String name; private int age; //@JsonIgnore//转化为JSON时忽略此属性 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date birthday; public User() { } // public String getBirStr(){ // if (this.birthday!=null) // return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.birthday); // return ""; // } public User(String name, int age, Date birthday) { this.name = name; this.age = age; this.birthday = birthday; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", birthday=" + birthday + '}'; } } package cn.itcast.web.json; import cn.itcast.web.domain.User; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test; import java.io.File; import java.io.IOException; import java.util.Date; public class JsonTest { @Test public void test1() throws IOException { User user = new User("张三",12,new Date()); ObjectMapper mapper = new ObjectMapper(); String s = mapper.writeValueAsString(user); mapper.writeValue(new File("F:\\a.txt"),user); System.out.println(s); } }

    JSON–解析器Jackson–java转json–List&Map

    @Test public void test2() throws IOException { List<User> users = new ArrayList<>(); users.add(new User("张三", 12, new Date())); users.add(new User("李四", 13, new Date())); users.add(new User("王五", 14, new Date())); ObjectMapper mapper = new ObjectMapper(); String s = mapper.writeValueAsString(users); //mapper.writeValue(new File("F:\\a.txt"),user); System.out.println(s); } @Test public void test3() throws IOException { HashMap<String, Object> usersMap = new HashMap<>(); usersMap.put("name","张三"); usersMap.put("age",12); usersMap.put("birthday",new Date()); ObjectMapper mapper = new ObjectMapper(); String s = mapper.writeValueAsString(usersMap); //mapper.writeValue(new File("F:\\a.txt"),user); System.out.println(s); }

    Processed: 0.013, SQL: 8