0

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"}
  ]

4
  • 1
    You should re-initialize the objects for each new object within the JSON. Move JSONObject series = new JSONObject(); JSONArray $_time = new JSONArray(); JSONArray msrVal = new JSONArray(); JSONArray unitVal = new JSONArray(); JSONObject msr = new JSONObject(); inside the for-loop Commented Apr 23, 2018 at 11:25
  • 2
    Are you sure about the title of the question? Where does the nullpointer happen? Commented Apr 23, 2018 at 11:25
  • I am so sorry, you are right, I mistake the title of the question, the real current problem is the duplicated output as in the description Commented Apr 23, 2018 at 11:31
  • Possible duplicate of Avoiding overwriting objects in ArrayList Commented Apr 23, 2018 at 12:03

1 Answer 1

1

You are not creating new JSON Objects for each iteration. You only create one at the beginning, set its values, and add it to the list. Then you overwrite the same object's values, and add it to the list again. In the end, you have added the object to the list three times, and its values will be the ones from the last iteration.

To fix this, change

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();
    ...
    measurements.add(msr);
}

to

for (int i=0; i<ts.length; i++) {
    JSONObject series = new JSONObject();
    JSONArray $_time = new JSONArray();
    JSONArray msrVal = new JSONArray();
    JSONArray unitVal = new JSONArray();
    JSONObject msr = new JSONObject();
    ...
    measurements.add(msr);
}

This way, you create a new object for every step and avoid changing the previous ones.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, I can't believe that the solution was as simple as that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.