JSON转对象 对象转JSON JSONArray

1.对象转JSON,属性为空的不显示

GeofenceCreateDTO geofenceDTO = new GeofenceCreateDTO();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        String jsonstr=mapper.writeValueAsString(geofenceDTO);

Bean对象添加注解:

 

 

@JsonInclude(JsonInclude.Include.NON_EMPTY)

//Include.Include.ALWAYS 默认 
//Include.NON_DEFAULT 属性为默认值不序列化 
//Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化 
//Include.NON_NULL 属性为NULL 不序列化 

 

 

 

 

 

2.JSON转对象

首先根据JSON生成对应的对象格式

 

String jso="{\"data\":{\"gid\":\"40affa1e-0e09-4d44-82bc-82e0fce1002b\",\"id\":\"0\",\"message\":\"成功\",\"status\":\"0\"},\"errcode\":0,\"errdetail\":null,\"errmsg\":\"OK\"}";
        JSONObject jsonObject=JSONObject.fromObject(jso);
        ResultMsgDTO stu=(ResultMsgDTO)JSONObject.toBean(jsonObject, ResultMsgDTO.class);
        System.out.println(stu);

 

3.JSON 转对象包含数组

JSON格式如下:

 

{
"data": {
"page_no": 1,
"page_size": 20,
"rs_list": [
      {
"adcode": "0",
"alert_condition": "",
"center": "116.48231,39.990177",
"create_time": "2017-01-16 17:49:51",
"enable": "false",
"fixed_date": "",
"gid": "7a6b40c6-7ba6-47e0-9b30-00354797c623",
"id": "0",
"key": "用户key",
"name": "fencetest",
"points": "",
"radius": 3000,
"repeat": "Mon",
"time": "11:30,13:15;14:00,17:45",
"valid_time": "2017-01-22"
}
],
"total_record": 1
},
"errcode": 0,
"errmsg": "OK"
}


根据JSON编写对应的实体

 

 

/**最外层*/
@Data
public class ResultMsgQueryDTO implements Serializable {
    private ResultDataQueryDTO data;
    private String errcode;
    private String errmsg;
    private String errdetail;

}
/**data数据层*/
@Data
public class ResultDataQueryDTO implements Serializable {
    private Integer page_no;
    private Integer page_size;
    private List<GeofenceDTO> rs_list;
    private Integer total_record;
}

 

/**List集合对象*/
@Data
public class GeofenceDTO implements Serializable {
    private String adcode;
    private String alert_condition;
    private String center;
    private String create_time;
    private Boolean enable;
    private String fixed_date;
    private String gid;
    private String id;
    private String key;
    private String name;
    private String points;
    private BigDecimal radius;
    private String repeat;
    private String time;
    private String valid_time;

}


解析JSON到对象如下

 

 

    public static void main(String[] args) throws Exception {
        String jso = "json";
        JSONObject jsonObject = JSONObject.fromObject(jso);
        ResultMsgQueryDTO resultMsgQueryDTO = (ResultMsgQueryDTO) JSONObject.toBean(jsonObject, ResultMsgQueryDTO.class);

        JSONObject data = jsonObject.getJSONObject("data");
        ResultDataQueryDTO resultDataQueryDTO = (ResultDataQueryDTO) JSONObject.toBean(data, ResultDataQueryDTO.class);
        JSONArray jsonArray = data.getJSONArray("rs_list");
        //参数1为要转换的JSONArray数据,参数2为要转换的目标数据,即List盛装的数据
        List<GeofenceDTO> list = JSONArray.toList(jsonArray, new GeofenceDTO(), new JsonConfig());
        resultDataQueryDTO.setRs_list(list);
        resultMsgQueryDTO.setData(resultDataQueryDTO);

        System.out.println(resultMsgQueryDTO);


    }

4.JSONArray套JSONArray,如何解析

 

String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'},{name:'d',value:'dd'}]" ;  // 一个未转化的字符串

JSONArray json = JSONArray.fromObject(str ); // 首先把字符串转成 JSONArray  对象

if(json.size()>0){
  for(int i=0;i<json.size();i++){
    JSONObject job = json.getJSONObject(i);  // 遍历 jsonarray 数组,把每一个对象转成 json 对象
    System.out.println(job.get("name")+"=") ;  // 得到 每个对象中的属性值
  }
}

 

导入包:

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

 

 

 


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值