JSON.toJSONString 首字母不小写

JSON.toJSONString 首字母不小写
强烈推介IDEA2021.1.3破解激活,IntelliJ IDEA 注册码,2021.1.3IDEA 激活码  

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说JSON.toJSONString 首字母不小写,希望能够帮助大家进步!!!

1、在字段上加注解@JSONField

  1. package com.cjh.wechatmp.po;

  2.  
  3. import com.alibaba.fastjson.annotation.JSONField;

  4. import lombok.Data;

  5. import lombok.NoArgsConstructor;

  6.  
  7. @NoArgsConstructor

  8. @Data

  9. public class InMessageText {

  10.  
  11. @JSONField(name = "ToUserName")

  12. private String ToUserName;

  13. @JSONField(name = "FromUserName")

  14. private String FromUserName;

  15. private String CreateTime;

  16. private String MsgType;

  17. private String Content;

  18. private String MsgId;

  19. }

优点:少量的话简单方便

缺点:入侵性强,懒得每个加

2、自定义配置

优点: 逼格高

缺点:并不是忽略不转,而是转了小写,再转回来大写

 
  1. package com.wechat.util;

  2.  
  3. import java.util.Map;

  4. import net.sf.json.JSONException;

  5. import net.sf.json.JSONObject;

  6. import net.sf.json.JsonConfig;

  7. import net.sf.json.util.JavaIdentifierTransformer;

  8. import net.sf.json.util.PropertySetStrategy;

  9.  
  10. public class JsonUtil {

  11.  
  12. public static Object toBean(JSONObject jsonObject, Class rootClass,

  13. String[] ignoreLowercase, Map<String, Class> childClass) {

  14. JsonConfig config = new JsonConfig();

  15.  
  16. //转换对象

  17. config.setRootClass(rootClass);

  18.  
  19. //首字母小写

  20. config.setJavaIdentifierTransformer(new JavaIdentifierTransformer() {

  21. @Override

  22. public String transformToJavaIdentifier(String str) {

  23. if (ignoreLowercase.length > 0) {

  24. for (String item : ignoreLowercase) {

  25. if (str.indexOf(item) == 0) {

  26. return str;

  27. }

  28. }

  29. }

  30. char[] chars = str.toCharArray();

  31. chars[0] = Character.toLowerCase(chars[0]);

  32. return new String(chars);

  33. }

  34. });

  35.  
  36. //对象里没有的属性忽略

  37. config.setPropertySetStrategy(new PropertySetStrategy() {

  38. @Override

  39. public void setProperty(Object o, String s, Object o1) throws JSONException {

  40. try {

  41. PropertySetStrategy.DEFAULT.setProperty(o, s, o1);

  42. } catch (JSONException e) {

  43. }

  44. }

  45. });

  46.  
  47. //自定义的list声明

  48. if (childClass != null) {

  49. config.setClassMap(childClass);

  50. }

  51.  
  52. return JSONObject.toBean(jsonObject, config);

  53. }

  54.  
  55. }

3、用Gson代替(推荐)

优点:简单方便

缺点:要添加依赖,不过不会和之前的冲突哦

 
  1. <!-- gson -->

  2. <dependency>

  3. <groupId>com.google.code.gson</groupId>

  4. <artifactId>gson</artifactId>

  5. <version>2.8.5</version>

  6. </dependency>

效果:

 
  1. InMessageText(ToUserName=toUser, FromUserName=fromUser, CreateTime=1348831860, MsgType=text, Content=this is a test, MsgId=1234567890123456)

  2. <xml><Content><![CDATA[this is a test]]></Content><CreateTime><![CDATA[1348831860]]></CreateTime><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[fromUser]]></FromUserName><MsgType><![CDATA[text]]></MsgType><MsgId><![CDATA[1234567890123456]]></MsgId></xml>

  3. Gson--->{"ToUserName":"toUser","FromUserName":"fromUser","CreateTime":"1348831860","MsgType":"text","Content":"this is a test","MsgId":"1234567890123456"}

  4. JSONObject--->{"content":"this is a test","createTime":"1348831860","fromUserName":"fromUser","msgId":"1234567890123456","msgType":"text","toUserName":"toUser"}

 

本文来源huayang183,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/17940

发表评论