I just starting working with the java JSON library (org.json.simple.JSONObject). In my code, I needed to make a JSONARRAY of JSONOBJECTS, however instead of finding the 3 different objects in my array, I find the the last object duplicated.
The function code:
private void setMesurement(Date[] ts, Integer[][] time, String mesurementLabel, Object[][] mesurementValue, Integer[][] unit ) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.FRENCH);
JSONArray measurements = new JSONArray();
JSONObject series = new JSONObject();
JSONArray $_time = new JSONArray();
JSONArray msrVal = new JSONArray();
JSONArray unitVal = new JSONArray();
JSONObject msr = new JSONObject();
for (int i=0; i<ts.length; i++) {
$_time.clear();
msrVal.clear();
unitVal.clear();
for (int j=0; j<time[i].length; j++) {
$_time.add(time[i][j]);
msrVal.add(mesurementValue[i][j]);
unitVal.add(unit[i][j]);
}
series.put("$_time", $_time);
series.put(mesurementLabel, msrVal);
series.put("unit", unitVal);
msr.put("series", series);
msr.put("ts", dateFormat.format(ts[i]));
measurements.add(msr);
}
this.ppmp.put("measurements", measurements);
System.out.println("adding measurements: "+measurements+" to ppmp");
}
and this is the duplicated result:
"measurements":[
{"series":{
"heat":[7,8,9],
"unit":[2,2,2],
"$_time":[0,16,36]},
"ts":"2018-04-23T11:30:35.587+0100"},
{"series":{
"heat":[7,8,9],
"unit":[2,2,2],
"$_time":[0,16,36]},
"ts":"2018-04-23T11:30:35.587+0100"},
{"series":{
"heat":[7,8,9],
"unit":[2,2,2],
"$_time":[0,16,36]},
"ts":"2018-04-23T11:30:35.587+0100"}
]
JSONObject series = new JSONObject(); JSONArray $_time = new JSONArray(); JSONArray msrVal = new JSONArray(); JSONArray unitVal = new JSONArray(); JSONObject msr = new JSONObject();inside the for-loop