关于FastJson bean中首字母大写与二层bean的问题。

前端之家收集整理的这篇文章主要介绍了关于FastJson bean中首字母大写与二层bean的问题。前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


1.首字母大写只要在bean前面get上加上

  1. @JsonProperty(value = "Name")
即可。

例如

  1. private String AppSysID;//APP版本号
  2. @JSONField(name = "AppSysID")
  3. public String getAppSysID() {
  4. return AppSysID;
  5. }
  6. public void setAppSysID(String AppSysID) {
  7. this.AppSysID = AppSysID;
  8. }


2.如果bean外面需要包一层。例如

  1. {"Bean":{ "AppSysID":"10.1" }}

则bean类为

  1. {"Bean":{ "AppSysID":"10.1" }}
  2.  
  3. public class DevInfo {
  4. public Bean Bean;
  5. public class Bean {
  6. private String AppSysID;//APP版本号
  7. @JSONField(name = "AppSysID")
  8. public String getAppSysID() {
  9. return AppSysID;
  10. }
  11. public void setAppSysID(String appSysID) {
  12. AppSysID = appSysID;
  13. }
  14.  
  15. }
  16. @JSONField(name = "Bean")
  17. public Bean getBean () {
  18. return Bean ;
  19. }
  20.  
  21. public void setBean (Bean getBean ) {
  22. this.Bean = getBean ;
  23. }
  24.  
  25. }

然后转成JSON
  1.  
  1. DevInfo devinfo = new DevInfo();
  2. Bean devSend = devinfo.new Bean();
  3. devSend.setAppSysID("1");
  4. devinfo.setBean(devSend);
  5. JSONObject devIDJSONObj = JSONObject.parSEObject(JSON.toJSONString(devinfo));
  6. String json = devIDJSONObj.toJSONString();
  1. System.out.println(json);
输出是这个:{"Bean":{"AppSysID":"1"}} =-=嗯就是这样了。。

猜你在找的Json相关文章