Reputation: 181
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:
{"sensors":[{},{"value":{}}]}
What might I be doing wrong?
Upvotes: 1
Views: 1751
Reputation: 1
Move below to setup() function
sensors_0["type"] = "co2";
sensors_0["value"] = 400;
Upvotes: 0