通过实体类转换JSON

  • RootEntity

        ` 
          package com.ithelei;
          import java.util.List;
      
          public class RootEntity {
      
          private String errorCode;
          private String errorMsg;
          private List<UserEntity> data;
      
          public String getErrorCode() {
              return errorCode;
          }
      
          public void setErrorCode(String errorCode) {
              this.errorCode = errorCode;
          }
      
          public String getErrorMsg() {
              return errorMsg;
          }
      
          public void setErrorMsg(String errorMsg) {
              this.errorMsg = errorMsg;
          }
      
          public List<UserEntity> getData() {
              return data;
          }
      
          public void setData(List<UserEntity> data) {
              this.data = data;
          }
      
          @Override
          public String toString() {
              return "RootEntity [errorCode=" + errorCode + ", errorMsg=" + errorMsg + ", data=" + data + "]";
          }
      
          }
          `
    
  • UserEntity

            `package com.ithelei;
      
           public class UserEntity {
      
          private String position;
          private String userName;
          private String webAddres;
      
          public String getPosition() {
              return position;
          }
      
          public void setPosition(String position) {
              this.position = position;
          }
      
          public String getUserName() {
              return userName;
          }
      
          public void setUserName(String userName) {
              this.userName = userName;
          }
      
          public String getWebAddres() {
              return webAddres;
          }
      
          public void setWebAddres(String webAddres) {
              this.webAddres = webAddres;
          }
      
          @Override
          public String toString() {
              return "UserEntity [position=" + position + ", userName=" + userName + ", webAddres=" + webAddres + "]";
          }
      
        }
        `
    
  • JSONTest

        `public static void main(String[] args) { 
         setBeanToJSON(); 
         }
         `

         `     
          public  static void setBeanToJSON() {
    
            RootEntity rootEntity = new RootEntity();
            rootEntity.setErrorCode("0");
            rootEntity.setErrorMsg("调用接口成功");
            
            List<UserEntity> data = new ArrayList<UserEntity>();
            
            UserEntity userEntity = new UserEntity();
            userEntity.setPosition("技术网站负责人");
            userEntity.setUserName("贺雷");
            userEntity.setWebAddres("www.ithelei.com");
            data.add(userEntity);//集合中装对象
            
            rootEntity.setData(data);
            System.out.println(new JSONObject().toJSONString(rootEntity));
        }`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值