JSON objects objects turn turn JSON JSONArray

1. Object turn JSON, the display attribute is not empty

GeofenceCreateDTO geofenceDTO = new GeofenceCreateDTO();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        String jsonstr=mapper.writeValueAsString(geofenceDTO);
Bean object add annotations:

@JsonInclude(JsonInclude.Include.NON_EMPTY)
//Include.Include.ALWAYS default 
//Include.NON_DEFAULT not attribute to the default value of the sequence 
//Include.NON_EMPTY property is empty ( "") is NULL or not serialized 
//Include.NON_NULL serialization attribute is not NULL 

2.JSON turn objects

Generating a corresponding first object format in accordance with 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 transfer object contains an array

JSON format is as follows:

{
"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"
}

The solid preparation of the corresponding 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;

}

To resolve the object as 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 sets JSONArray, how to resolve

String str = "[{name: 'a', value: 'aa'}, {name: 'b', value: 'bb'}, {name: 'c', value: 'cc'}, {name: 'd', value: 'dd'}] "; a // string unconverted

JSONArray json = JSONArray.fromObject (str); // string is first converted into the target JSONArray

IF (json.size ()> 0) {
  for (int I = 0; I <json.size (); I ++) {
    the JSONObject Job = json.getJSONObject (I); // iterate jsonarray array, each of the object- json objects into
    System.out.println (job.get ( "name") + "="); // attribute value obtained in each object
  }
}






Guess you like

Origin blog.csdn.net/lanqibaoer/article/details/78730289