import org.apache.commons.lang.StringEscapeUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.demo.work.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonToObject {
private static final ObjectMapper simple_mapper = new ObjectMapper();
public static void main(String[] args) throws JsonProcessingException {
//对象转json
User userInfo = new User();
userInfo.setAge(1);
ObjectMapper simple_mapper = new ObjectMapper();
String userJson = simple_mapper.writeValueAsString(userInfo);
System.out.println(userJson);
//或者是下面这样
Object o = JSON.toJSON(userInfo );
//json转对象
System.out.println("------------------------");
User userdo = JSON.parseObject(StringEscapeUtils.unescapeJava(userJson), new TypeReference<User>() {
});
System.out.println("1111111111111111");
}
}
json和对象互相转换 java
最新推荐文章于 2024-06-27 09:28:04 发布