POST fastJSON List<H>

public static HashMap<String, Object> fromJson2Map(String jsonString) {  
    HashMap jsonMap = JSON.parseObject(jsonString, HashMap.class);  
  
    HashMap<String, Object> resultMap = new HashMap<String, Object>();  
    for(Iterator iter = jsonMap.keySet().iterator(); iter.hasNext();){  
        String key = (String)iter.next();  
        if(jsonMap.get(key) instanceof JSONArray){  
            JSONArray jsonArray = (JSONArray)jsonMap.get(key);  
            List list = handleJSONArray(jsonArray);  
            resultMap.put(key, list);  
        }else{  
            resultMap.put(key, jsonMap.get(key));  
        }  
    }  
    return resultMap;  
}  
  
private static List<HashMap<String, Object>> handleJSONArray(JSONArray jsonArray){  
    List list = new ArrayList();  
    for (Object object : jsonArray) {  
        JSONObject jsonObject = (JSONObject) object;  
        HashMap map = new HashMap<String, Object>();  
        for (Map.Entry entry : jsonObject.entrySet()) {  
            if(entry.getValue() instanceof  JSONArray){  
                map.put((String)entry.getKey(), handleJSONArray((JSONArray)entry.getValue()));  
            }else{  
                map.put((String)entry.getKey(), entry.getValue());  
            }  
        }  
        list.add(map);  
    }  
    return list;  
}  

 

{"subList":[{"material_id":5,"isHave":1,"isPractical":2}]}
 JSONArray subList = JSON.parseObject(jsonSubList, JSONArray.class);
    List<HashMap<String, Object>> configDataList = handleJSONArray(subList);

假设NotifyInfoCCResp类中包含有flightId、flightDate、flightNo等属性,那么可以使用以下代码将返回结果中的data值转换为List<NotifyInfoCCResp>: ``` import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { // 假设返回的json字符串为responseStr String responseStr = "{\"code\":\"000\",\"message\":\"成功\",\"data\":[{\"flightId\":1831011835,\"flightDate\":\"2023-05-02 00:00:00\",\"flightNo\":\"2154\",\"departCode\":\"SHA\",\"arriveCode\":\"XIY\",\"carrier\":\"MU\",\"flightStatus\":\"FINISH\",\"planDepartTime\":\"2023-05-02 12:25:00\",\"planArriveTime\":\"2023-05-02 14:45:00\",\"createTime\":\"2023-04-26 14:57:58\",\"flightType\":\"CNL\",\"source\":\"MANUAL\"}],\"success\":true}"; // 将json字符串转换为JSONObject对象 JSONObject responseObj = JSON.parseObject(responseStr); // 从JSONObject对象中获取data值 JSONArray dataArray = responseObj.getJSONArray("data"); // 将data值转换为List<NotifyInfoCCResp> List<NotifyInfoCCResp> dataList = new ArrayList<>(); for (int i = 0; i < dataArray.size(); i++) { JSONObject dataObj = dataArray.getJSONObject(i); NotifyInfoCCResp notifyInfoCCResp = new NotifyInfoCCResp(); notifyInfoCCResp.setFlightId(dataObj.getLong("flightId")); notifyInfoCCResp.setFlightDate(dataObj.getString("flightDate")); notifyInfoCCResp.setFlightNo(dataObj.getString("flightNo")); notifyInfoCCResp.setDepartCode(dataObj.getString("departCode")); notifyInfoCCResp.setArriveCode(dataObj.getString("arriveCode")); notifyInfoCCResp.setCarrier(dataObj.getString("carrier")); notifyInfoCCResp.setFlightStatus(dataObj.getString("flightStatus")); notifyInfoCCResp.setPlanDepartTime(dataObj.getString("planDepartTime")); notifyInfoCCResp.setPlanArriveTime(dataObj.getString("planArriveTime")); notifyInfoCCResp.setCreateTime(dataObj.getString("createTime")); notifyInfoCCResp.setFlightType(dataObj.getString("flightType")); notifyInfoCCResp.setSource(dataObj.getString("source")); dataList.add(notifyInfoCCResp); } // 打印List<NotifyInfoCCResp>中的数据 for (NotifyInfoCCResp notifyInfoCCResp : dataList) { System.out.println(notifyInfoCCResp); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值