2019-04-22 16:11:33 卢浮宫 版权声明:本文为站长原创文章,转载请写明出处
最近频繁用到阿里巴巴的fastJson,所以在这里做一个小结。
① JSONObject obj = new JSONObject();
② JSONObject obj = JSON.parseObject("你的JSON字符串");
FlightSearch flightSearch = new FlightSearch();
flightSearch.setArr(arr);
flightSearch.setDpt(dpt);
flightSearch.setDate(date);
flightSearch.setEx_track(ex_track);
buildMap(0,JSONObject.toJSONString(flightSearch));
实体类 javaBean = JSON.parseObject(json, 实体类.class);FlightSearch flightSearch1 = JSON.parseObject(JSONObject.toJSONString(flightSearch),flightSearch.getClass());
五、JSONArray相关
① JSONArray jsonArr = JSON.parseArray("你的json字符串");②遍历private static boolean pridocutListFormat(JSONArray productArr,DBCollection collection)throws UnknownHostException {
// 获取第一层的productId并传递给子项 -- 这里过滤国际航班 INTERNATIONAL_FLAG = 2
boolean rlt = true;
try {
for (int i = 0; i < productArr.size(); i++) {
JSONObject jsonObj = JSON.parseObject(productArr.get(i).toString());
String internationCode = jsonObj.getString("INTERNATIONAL_FLAG");
if (internationCode.equals("1")) {
String productIdTmp = jsonObj.getString("PRODUCT_ID");
getAirDetailByProductId(productIdTmp, collection);
}
}
} catch (Exception e) {
ExceptionUtil.logException("获取并解析productId出现异常:", e);
return false;
} finally {
MongoDBUtil.mongoDbDisConnect();
}
return rlt;
}