JsonParserGeneratorRK icon indicating copy to clipboard operation
JsonParserGeneratorRK copied to clipboard

Generating Array of Objects

Open CaptainMack opened this issue 5 years ago • 3 comments

Hi,

I am trying to achieve the following JSON object:

{
	"b": 92.60,
	"SDI12-Id": "113CampbellCS65X 000Std.02=37053",
	"seq": 666,
	"t": -99.99,
	"Time": "2020-08-31T12:44:24Z",
	"Pairs": [{
		"ParameterID": "0",
		"Value": 0.00
	}, {
		"ParameterID": "1",
		"Value": 0.00
	}, {
		"ParameterID": "2",
		"Value": 22.33
	}]
}

However, I am getting the following object instead - without the comma separators between the JSON objects in the Pairs array:

{
	"b": 92.60,
	"SDI12-Id": "113CampbellCS65X 000Std.02=37053",
	"seq": 666,
	"t": -99.99,
	"Time": "2020-08-31T12:44:24Z",
	"Pairs": [{
		"ParameterID": "0",
		"Value": 0.00
	} {
		"ParameterID": "1",
		"Value": 0.00
	} {
		"ParameterID": "2",
		"Value": 22.33
	}]
}

And I have the following code generating the JSON object:

JsonWriterStatic<622> jw;
      {
        JsonWriterAutoObject obj(&jw);
        jw.setFloatPlaces(2);
        jw.insertKeyValue("b", fuel.getSoC());                  
        jw.insertKeyValue("SDI12-Id", String(sensorIDs[sensor]).trim()); 
        jw.insertKeyValue("seq", seq);                                  
        jw.insertKeyValue("t", getTemperature());                       
        jw.insertKeyValue("Time", Time.format(TIME_FORMAT_ISO8601_FULL)); 
        jw.insertKeyArray("Pairs");                                      
        for (int n = 0; n < SDI.sensorCount; n++)
        {
          jw.startObject();
          jw.insertKeyValue("ParameterID", String(n));
          jw.insertKeyValue("Value", readings[n]);
          jw.finishObjectOrArray();
        }
        jw.insertCheckSeparator();
        jw.finishObjectOrArray();
      }

CaptainMack avatar Aug 31 '20 12:08 CaptainMack

@CaptainMack did you manage to create a workaround for this issue?

tiborrr avatar Jan 19 '21 09:01 tiborrr

Hello @tiborrr

It seems like I did! I had to go back to check what code I ended up with, the following is on our production units:

      JsonWriterStatic<622> jw;
      {
        JsonWriterAutoObject obj(&jw);
        jw.setFloatPlaces(2);
        jw.insertKeyValue("b", System.batteryCharge());                   // battery state of charge
        jw.insertKeyValue("SDI12-Id", String(sensorIDs[sensor]).trim());  // SDI-12 Sensor Identification
        jw.insertKeyValue("seq", seq);                                    // sequence number
        jw.insertKeyValue("t", getTemperature());                         // temperature in Celcius
        jw.insertKeyValue("Time", Time.format(TIME_FORMAT_ISO8601_FULL)); // Timestamp for measurement
        jw.insertKeyArray("Pairs");                                       // Start Pairs array
        for (int n = 0; n < SDI.sensorCount; n++)
        {
          jw.insertCheckSeparator();
          jw.startObject();
          jw.insertKeyValue("ParameterID", String(n));
          jw.insertKeyValue("Value", SDI.sensorReadings[n]);
          jw.finishObjectOrArray();
        }
        jw.finishObjectOrArray();
      }

Let me know if this works for you!

CaptainMack avatar Jan 19 '21 10:01 CaptainMack

I already have found a workaround that works for me. Might be useful for the next one stumbling on this problem :). Thanks for letting me know!

tiborrr avatar Jan 19 '21 10:01 tiborrr