zoukankan      html  css  js  c++  java
  • jackson json转对象 json转集合 对大小写支持

     1     @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE)
     2     public static class XnServiceGetUserTest {
     3         @NotEmpty(message = "票据凭证不能为空")
     4         private String Token;
     5 
     6         @NotEmpty(message = "用户号不能为空")
     7         private String PERNR;
     8 
     9         @NotEmpty(message = "操作类型不能为空")
    10         private String OPTYPE = "QRY";
    11 
    12         public String getToken() {
    13             return Token;
    14         }
    15 
    16         public void setToken(String token) {
    17             Token = token;
    18         }
    19 
    20         public String getPERNR() {
    21             return PERNR;
    22         }
    23 
    24         public void setPERNR(String pERNR) {
    25             PERNR = pERNR;
    26         }
    27 
    28         public String getOPTYPE() {
    29             return OPTYPE;
    30         }
    31 
    32         public void setOPTYPE(String oPTYPE) {
    33             OPTYPE = oPTYPE;
    34         }
    35 
    36     }

    -----------json转对象和对象转json

     1     public static void main(String[] args) throws Exception {
     2         String json = "{"OPTYPE":"QRY","Token":"cRQENwe3AYNpTlz7e0dxag==","PERNR":"aaa"}";
     3         ObjectMapper mapper = new ObjectMapper();
     4      //json转对象
     5         XnServiceGetUserTest xnServiceGetUserTest = mapper.readValue(json, XnServiceGetUserTest.class);
     6 
     7         System.out.println(xnServiceGetUserTest.OPTYPE);
     8         System.out.println(xnServiceGetUserTest.PERNR);
     9         System.out.println(xnServiceGetUserTest.Token);
    10      //对象转json
    11         System.out.println(mapper.writeValueAsString(xnServiceGetUserTest));
    12 
    13     }

    -------------json集合 转对象

     1     public static void main(String[] args) throws Exception {
     2 
     3         Aaw aaw = new Aaw();
     4         List a = new ArrayList();
     5         a.add(aaw);
     6         aaw.setWocao("xxx");
     7         a.add(aaw);
     8 
     9         System.out.println(objectMapper.writeValueAsString(a));
    10 
    11         List<Aaw> bb = objectMapper.readValue("[{"nn":null,"wocao":"xxx"},{"nn":null,"wocao":"xxx"}]",
    12                 new TypeReference<List<Aaw>>() {
    13                 });
    14 
    15         System.out.println(bb);
    16 
    17     }
    注意:@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE)
    这个是用于设置属性的自动发现,这样就可以支持大小写,避免多生成小写。另外如果java属性名未按照规范命名,如:aaa_aa,可用该注解来直接读取属性生成json。


  • 相关阅读:
    Java多线程总结之线程安全队列Queue
    Java四种线程池
    Java多线程同步 synchronized 关键字的使用
    mysql存储过程语法及实例
    Mysql 中有关日期的函数(sql)
    mysql sql常用语句大全
    spring主要的作用?
    Java:描述反射机制的作用?举几个反射的应用?
    设计模式之六大原则(转载)
    Android之微信开放平台创建应用
  • 原文地址:https://www.cnblogs.com/wulm/p/7884897.html
Copyright © 2011-2022 走看看