1. 创建实体类对象 
@Data
public class ResponseInfo {
    
    private String orderId;
    
     enum  result{
        成功("1"),失败("2");
        private String value;
        private  result(String value){
            this.value = value;
        }
        public String getValue(){
            return this.value;
        }
    }
    
    private String errorMsg;
}
  
给对象赋值,并转为JSON(对象转JSON) 
 ResponseInfo responseInfo = new ResponseInfo();
 responseInfo.setOrderId("2222222");
 responseInfo.setErrorMsg("你错了,错误在这里");
 String result = JSONObject.toJSONString(responseInfo);
 System.out.println("=========="+result);
  
输出结果为: 
   
将JSON转为对象(JSON转为对象) 
准备一个接收的对象,需要注意的是:接收对象内的字段与JSON字符串里面的字段需要一一对应,可以有多余的字段,如下: 
@Data
public class ResponseInfoDto {
    
    private String orderId;
    
     enum  result{
        成功("1"),失败("2");
        private String value;
        private  result(String value){
            this.value = value;
        }
        public String getValue(){
            return this.value;
        }
    }
    
    private String errorMsg;
    private String msg;
}
  
实现JSONObject.parseObject(JSON字符串,接收对象.class); 
ResponseInfoDto response = JSONObject.parseObject(result,ResponseInfoDto.class);
System.out.println("************"+response);
  
输出结果 
  
                
                
                
        
        
    
  
 
 |