zoukankan      html  css  js  c++  java
  • jackson与fastjson的使用

    jackson与fastjson的使用

    jackson

    1. 导包

      <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.10.1</version>
      </dependency>
      
    2. 处理乱码

    3. json解析

      //@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<User> userList = new ArrayList<User>();
              User user1 = new User("大头儿子", 6, "男");
              User user2 = new User("也是大头儿子", 6, "男");
              User user3 = new User("还是大头儿子", 6, "男");
              User user4 = new User("不是大头儿子", 6, "女");
              userList.add(user1);
              userList.add(user2);
              userList.add(user3);
              userList.add(user4);
              String users = mapper.writeValueAsString(userList);
              return users;
          }
      
          @RequestMapping("/json3")
          //时间
          public String json3() throws JsonProcessingException {
              ObjectMapper mapper = new ObjectMapper();
              Date oridate = new Date();
              //通过java格式化时间
              SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
              String date = format.format(oridate);
              String obj = mapper.writeValueAsString(date);
              return obj;
          }
      
          @RequestMapping("/json4")
          public String json4() throws JsonProcessingException {
              ObjectMapper mapper = new ObjectMapper();
              Date oridate = new Date();
              //不使用时间戳的方式
              mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
              //自定义时间格式
              SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
              mapper.setDateFormat(format);
              String obj = mapper.writeValueAsString(oridate);
              return obj;
          }
      
          @RequestMapping("/json5")
          public String json5() throws JsonProcessingException {
              Date oridate = new Date();
              //使用自建工具类
              String obj = JsonUtils.getJson(oridate);
              return obj;
          }
      
      
          @RequestMapping("/json6")
          //队列
          public String json6() throws JsonProcessingException {
              List<User> userList = new ArrayList<User>();
              User user1 = new User("大头儿子", 6, "男");
              User user2 = new User("也是大头儿子", 6, "男");
              User user3 = new User("还是大头儿子", 6, "男");
              User user4 = new User("不是大头儿子", 6, "女");
              userList.add(user1);
              userList.add(user2);
              userList.add(user3);
              userList.add(user4);
              //使用自建工具类
              String users = JsonUtils.getJson(userList);
              return users;
          }
      
    4. 自建工具类

      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;
          }
      }
      

    fastjson

    1. 导包

      <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.2.62</version>
      </dependency>
      
    2. json解析

      @RequestMapping("/json7")
      //队列
      public String json7() throws JsonProcessingException {
          List<User> userList = new ArrayList<User>();
          User user1 = new User("大头儿子", 6, "男");
          User user2 = new User("也是大头儿子", 6, "男");
          User user3 = new User("还是大头儿子", 6, "男");
          User user4 = new User("不是大头儿子", 6, "女");
          userList.add(user1);
          userList.add(user2);
          userList.add(user3);
          userList.add(user4);
          String users = JSON.toJSONString(userList);
          return users;
      }
      
    3. 更多

      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("
      ****** JSON字符串 转 Java对象*******");
      User jp_user1=JSON.parseObject(str2,User.class);
      System.out.println("JSON.parseObject(str2,User.class)==>"+jp_user1);
      
      System.out.println("
      ****** Java对象 转 JSON对象 ******");
      JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2);
      System.out.println("(JSONObject) JSON.toJSON(user2)==>"+jsonObject1.getString("name"));
      
      System.out.println("
      ****** JSON对象 转 Java对象 ******");
      User to_java_user = JSON.toJavaObject(jsonObject1, User.class);
      System.out.println("JSON.toJavaObject(jsonObject1, User.class)==>"+to_java_user);
      
  • 相关阅读:
    【转】全文检索引擎Sphinx配置文件详细介绍
    【转】构建不依赖于cookie的手机端用户登录机制
    Sphinx在window下的初步安装和配置
    Zend Framework 在.htaccess中修改RewriteRule实现url重写
    做后台的程序猿的一点心得
    [Leetcode 75] 96 Unique Binary Search Trees
    [Leetcode 74] 92 Restore IP Addresses
    [Leetcode 73] 92 Reverse Linked List II
    [Leetcode 72] 91 Decode Ways
    [Leetcode 71] 86 Partition List
  • 原文地址:https://www.cnblogs.com/pinked/p/12230005.html
Copyright © 2011-2022 走看看