1. 程式人生 > >JSON轉換成實體類(實體類巢狀多層list)

JSON轉換成實體類(實體類巢狀多層list)

在專案中,rabbitMQ返回的不是JSON而是二進位制數

json jar :

import com.alibaba.fastjson.JSON;

import net.sf.json.JSONObject;

@MessageQueueListener(queues = "check_result_v1_queue_test0530")

public void process(byte[] result) {
String json = "";
try {
System.out.println(new String(result,"UTF-8"));
json = new String(result,"UTF-8");

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
JSONObject jsonObj = JSONObject.fromObject(json);    
                Map<String, Class> classMap = new HashMap<String, Class>();    
                classMap.put("hosts", AutoResultHostMQDTO.class);      
                classMap.put("items", AutoResultItemsDTO.class);      

AutoResultMQDTO  dto = (AutoResultMQDTO) JSONObject.toBean(jsonObj, AutoResultMQDTO.class, classMap);  

 }

實體類: