Looking for some advice, I'm trying to work with the ArduinoJson library. The problem is with the code listed below:
#include <ArduinoJson.h>
const size_t capacity = JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(1) + 3*JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(6);
DynamicJsonDocument data(capacity);
JsonObject root = data.to<JsonObject>();
JsonArray sensors = root.createNestedArray("sensors");
JsonObject sensors_0 = sensors.createNestedObject();
sensors_0["type"] = "co2";
sensors_0["value"] = 400;
JsonObject gps_obj = sensors.createNestedObject();
JsonObject gps_value_obj = gps_obj.createNestedObject("value");
void setup() {
Serial.begin(115200);
serializeJson(data, Serial);
}
void loop() {
// not used
}
The error:
'sensors_0' does not name a type
This follows the documentation code at https://arduinojson.org/v6/api/jsonobject/createnestedobject/
Things I've tried:
- Tried code directly from the documentation above (get the same error)
- Checked the library folder for directory structure repetition.
- If I remove the error lines (sensors_0["type"] = "co2" and sensors_0["value"] = 400) I get the exact JSON structure I desire without the keys:
{"sensors":[{},{"value":{}}]}
What might I be doing wrong?
Move below to setup() function
sensors_0["type"] = "co2";
sensors_0["value"] = 400;