package com.itheima.action;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Test;
public class JacksonTest {
@Test
public void test1() throws Exception {
User user = new User("张三", 20, new Date());
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
String json = mapper.writeValueAsString(user);
System.out.println(json);
}
@Test
public void test2() throws Exception {
User user1 = new User("张三", 20, new Date());
User user2 = new User("李四", 18, new Date());
List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
String json = mapper.writeValueAsString(users);
System.out.println(json);
}
}
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Test;
public class JacksonTest {
@Test
public void test1() throws Exception {
User user = new User("张三", 20, new Date());
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
String json = mapper.writeValueAsString(user);
System.out.println(json);
}
@Test
public void test2() throws Exception {
User user1 = new User("张三", 20, new Date());
User user2 = new User("李四", 18, new Date());
List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
String json = mapper.writeValueAsString(users);
System.out.println(json);
}
}