jackson与fastjson的使用
2021-04-25 12:26
标签:sys try stack use date cti json 视图 pos 导包 处理乱码 json解析 自建工具类 导包 json解析 更多 jackson与fastjson的使用 标签:sys try stack use date cti json 视图 pos 原文地址:https://www.cnblogs.com/pinked/p/12230005.htmljackson与fastjson的使用
jackson
//@Controller
@RestController//不经过视图解析器,return字符串
public class UserController {
@RequestMapping("/json1")
//@ResponseBody//不经过视图解析器,return字符串
public String json1() throws JsonProcessingException {
//jackson - ObjectMapper
ObjectMapper mapper = new ObjectMapper();
User user = new User("大头儿子", 6, "男");
String string = mapper.writeValueAsString(user);
return string;
}
@RequestMapping("/json2")
//队列
public String json2() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
List
public class JsonUtils {
public static String getJson(Object object) {
return getJson(object, "yy-MM-dd HH:mm:ss");
}
public static String getJson(Object object, String dateformat) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
SimpleDateFormat format = new SimpleDateFormat(dateformat);
mapper.setDateFormat(format);
try {
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
}
fashjson
@RequestMapping("/json7")
//队列
public String json7() throws JsonProcessingException {
List
System.out.println("*******Java对象 转 JSON字符串*******");
String str1 = JSON.toJSONString(users);
System.out.println("JSON.toJSONString(list)==>"+str1);
String str2 = JSON.toJSONString(user1);
System.out.println("JSON.toJSONString(user1)==>"+str2);
System.out.println("\n****** JSON字符串 转 Java对象*******");
User jp_user1=JSON.parseObject(str2,User.class);
System.out.println("JSON.parseObject(str2,User.class)==>"+jp_user1);
System.out.println("\n****** Java对象 转 JSON对象 ******");
JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2);
System.out.println("(JSONObject) JSON.toJSON(user2)==>"+jsonObject1.getString("name"));
System.out.println("\n****** JSON对象 转 Java对象 ******");
User to_java_user = JSON.toJavaObject(jsonObject1, User.class);
System.out.println("JSON.toJavaObject(jsonObject1, User.class)==>"+to_java_user);
文章标题:jackson与fastjson的使用
文章链接:http://soscw.com/index.php/essay/79380.html