对象转json的测试

使用到的jar包如下:

对象转json的测试

 

 测试代码如下:

package proxy;

import java.util.Date;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.AllArgsConstructor;
import lombok.Data;

public class Test {
@Data
@AllArgsConstructor
class User {
private String name;
private int age;
private Date date;
}

public static void main(String[] args) {
User user = new Test().new User("jerry", 20, new Date());
String json = "";
ObjectMapper mapper = new ObjectMapper();
try {
json = mapper.writeValueAsString(user);
System.out.println(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}

}

输出结果:

对象转json的测试

 

上一篇:Ajax


下一篇:java JSON转换