maven项目pom配置:
net.sf.json-lib
json-lib
2.4
jdk15
importnet.sf.json.JSONObject;//报文
String report="{\"SEND_TIME\":\"20170509235000\",\"AREA_SOURCE\":\"ZSQD\",\"ACID\":\"QDA9772\",\"TASK\":\"W/Z\","
+ "\"PLAN_SOURCE\":\"QDNEXTPLAN\",\"ADEP\":\"ZUUU\",\"ADES\":\"ZSQD\",\"STOD\":\"0240\",\"STOA\":\"0525\","
+ "\"ETOT\":\"0240\",\"ELDT\":\"0525\",\"AIRCRAFT_TYPE\":\"A320\",\"REGID\":\"B1648\",\"EXECUTE_DATE\":\"20170510\"}";//转json对象(反序列化)
JSONObject jsonObject =JSONObject.fromObject(report);//强转成PlanReport类
PlanReport hbreport=(PlanReport)JSONObject.toBean(jsonObject,PlanReport.class);
//序列化
jsonObject.toString();
//json合并 相同的字段会被后一个覆盖
JSONObject oldjson1=newJSONObject();
oldjson1.put("name", "zhangsan");
JSONObject oldjson2=newJSONObject();
oldjson2.put("name", "lisi");
oldjson2.put("age", "20");//合并后的json
JSONObject newjson=newJSONObject();
newjson.putAll(oldjson1);
newjson.putAll(oldjson2);
json对比:
/***
* 对比json对象中不同的value
**/
public staticString compareJson(JSONObject json1, JSONObject json2,String key) {
String str="";
@SuppressWarnings("unchecked")
Iterator i =json1.keys();while(i.hasNext()) {
key=i.next();
str+=compareJson(json1.get(key), json2.get(key),key);
}returnstr;
}public staticString compareJson(Object json1,Object json2,String key) {
String str="";if ( json1 instanceofJSONObject ) {
str=compareJson((JSONObject) json1 ,(JSONObject) json2,key);
}else if ( json1 instanceofJSONArray ) {
compareJson((JSONArray) json1 ,(JSONArray) json2,key);
}else if(json1 instanceofString ){
str=compareJson((String) json1 ,(String) json2,key);
}else{
str=compareJson(json1.toString(), json2.toString(), key);
}returnstr;
}public staticString compareJson(String str1,String str2,String key) {
String str="";if (!str1.equals(str2)&& str2!=null) {//System.out.println("字段更新:"+key+ ",原报文数据:"+ str1 +",新报文数据:"+str2);
str="字段更新:"+key+ ",原报文数据:"+ str1 +",新报文数据:"+str2+" ";
}returnstr;
}public static voidcompareJson(JSONArray json1,JSONArray json2,String key) {
Iterator> i1=json1.iterator();
Iterator> i2=json2.iterator();while( i1.hasNext()) {
compareJson(i1.next(), i2.next(),key);
}
}
json中不同字段对比,可以先转换成类再用反射技术进行对比
//通过反射获取到不同的属性
List textList =Lists.newArrayList();
String jsonstr="";try{
Class extends PlanReport> clazz =hbreport.getClass();
Field[] fields=hbreport.getClass().getDeclaredFields();for(Field field : fields) {
PropertyDescriptor pd= newPropertyDescriptor(field.getName(), clazz);
Method getMethod=pd.getReadMethod();
Object o1=getMethod.invoke(oldreport);
Object o2=getMethod.invoke(hbreport);
String s1= o1 == null ? "" : o1.toString();//避免空指针异常
String s2 = o2 == null ? "" : o2.toString();//避免空指针异常
if (!s1.contains(s2)) {
textList.add("字段新增:" + field.getName() + " 字段值:" + s2 + " ");
}
}
}catch(Exception e) {
logger.info("计划日志字段新增反射异常日志打印 [{}]", e);
System.out.println(e.getMessage());
}for(String object : textList) {
jsonstr+=object;
}
JSONArray遍历:转换Iterator(迭代器)进行遍历
String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'},{name:'d',value:'dd'}]";
JSONArray jsonarr=JSONArray.fromObject(str );
Iterator it =jsonarr.iterator();while(it.hasNext()) {
JSONObject jsonObject=(JSONObject) it.next();
System.out.println(jsonObject.toString());
}
JSONArray 中按照某个Key排序:
JSONArray JsonArray=newJSONArray();
JsonArray.add(***);
//从小到大
JsonArray.sort(new Comparator() {
@Overridepublic intcompare(JSONObject o1, JSONObject o2) {return o1.getString("SEND_TIME").compareTo(o2.getString("SEND_TIME"));
}
});