我遇到一个问题,当我尝试合并JSON时遇到错误:java.lang.NullPointerException。
我有一个JSON对象:mergedJSON2,在合并到另一个JSON对象之前,我需要获取值,进行重构和排序。错误发生在以下代码行:
mergedJSONDataLog = mergeJSONObjects(mergedJSONDataLog,mergedJSON3);
我已经按照如下方式使用了合并功能:
public static JSONObject mergeJSONObjects(JSONObject json1, JSONObject json2) {
JSONObject mergedJSON = new JSONObject();
try {
mergedJSON = new JSONObject(json1, JSONObject.getNames(json1));
for (String crunchifyKey : JSONObject.getNames(json2)) {
mergedJSON.put(crunchifyKey, json2.get(crunchifyKey));
}
} catch (JSONException e) {
throw new RuntimeException("JSON Exception" + e);
}
return mergedJSON;
}
代码如下:
System.out.println("JSON DATA is: "+mergedJSON2);
JSONObject mergedJSON3 = new JSONObject();
JSONObject mergedJSONDataLog = null;
for(int uT=1; uT < lengthTime+1; uT++) {
String str = Integer.toString(uT);
JSONArray arrTime=(JSONArray)mergedJSON2.get(str);
JSONObject objTime;
String timeLoginManipulate,timeInsertManipulate,browserInfoManipulate, browserOSNameManipulate,
ipIntManipulate, latitudeManipulate, longitudeManipulate, spIDManipulate;
for (Object roTime : arrTime) { //roTime
objTime = (JSONObject) roTime;
timeInsertManipulate = String.valueOf(objTime.get("loginTime"));
if (timeInsertManipulate != sTime) {
timeLoginManipulate = String.valueOf(objTime.get("timeLogin"));
browserInfoManipulate = String.valueOf(objTime.get("browserInfo"));
browserOSNameManipulate = String.valueOf(objTime.get("browserOSName"));
ipIntManipulate = String.valueOf(objTime.get("ipInt"));
latitudeManipulate = String.valueOf(objTime.get("latitude"));
longitudeManipulate = String.valueOf(objTime.get("longitude"));
spIDManipulate = String.valueOf(objTime.get("spID"));
JSONArray jArrayDataLogManipulate = new JSONArray();
JSONObject objManipulate = new JSONObject();
objManipulate.put("timeLogin", timeLoginManipulate);
objManipulate.put("browserInfo", browserInfoManipulate);
objManipulate.put("browserOSName", browserOSNameManipulate);
objManipulate.put("ipInt", ipIntManipulate);
objManipulate.put("latitude", latitudeManipulate);
objManipulate.put("longitude", longitudeManipulate);
objManipulate.put("spID", spIDManipulate);
jArrayDataLogManipulate.put(objManipulate);
JSONObject jObjDataLogManipulate = new JSONObject();
jObjDataLogManipulate.put(str, jArrayDataLogManipulate);
mergedJSON3 = jObjDataLogManipulate;
mergedJSONDataLog = mergeJSONObjects(mergedJSONDataLog, mergedJSON3);
}
}//end for roTime
} //end for uT