Hello
I'm having some difficulties extracting a specific type of JSON and storing them in variables.
The JSON is in this format:
{"BLOCKA": {"UNIT1": {"names":["Filipe","Ana"]},"UNIT2": {"names":["Lorraine","Matheus"]}},"BLOCKB": {"UNIT3": {"names":["Filipe","Ana"]},"UNIT4": {"names":["Lorraine","Matheus"]}}}
There are fields called "BLOCKS", inside each block there are other fields called unit and inside each unit is an array of names. Therefore, I would need to extract the data accordingly. For example, I would need to access the names that are within units of a certain block, but I'm having a lot of difficulty doing this.
Through the Arduino Json website, in the assistant, it generates a code for me but the code is based on that exact amount of information, but I would like to do it with any amount of data, so I can't work the code generated by them.
Code generated by Arduino Json assistant:
// String input;
StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, input);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
for (JsonPair BLOCKA_item : doc["BLOCKA"].as<JsonObject>()) {
const char* BLOCKA_item_key = BLOCKA_item.key().c_str(); // "UNIT1", "UNIT2"
const char* BLOCKA_item_value_names_0 = BLOCKA_item.value()["names"][0]; // "Filipe", "Lorraine"
const char* BLOCKA_item_value_names_1 = BLOCKA_item.value()["names"][1]; // "Ana", "Matheus"
}
for (JsonPair BLOCKB_item : doc["BLOCKB"].as<JsonObject>()) {
const char* BLOCKB_item_key = BLOCKB_item.key().c_str(); // "UNIT3", "UNIT4"
const char* BLOCKB_item_value_names_0 = BLOCKB_item.value()["names"][0]; // "Filipe", "Lorraine"
const char* BLOCKB_item_value_names_1 = BLOCKB_item.value()["names"][1]; // "Ana", "Matheus"
}
My code:
#include "ArduinoJson.h"
String input =
"{\"BLOCKA\": {\"101\": {\"names\":[\"Filipe\",\"Ana\"]},\"102\": {\"names\":[\"Lorraine\",\"Matheus\"]}},\"BLOCKB\": {\"201\": {\"names\":[\"Claudia\",\"Carla\"]},\"202\": {\"names\":[\"Rodolfo\",\"Cleber\"]}}}";
class Unit
{
public:
String building;
String unit[];
String names[][99];
};
Unit units[99];
void setup() {
Serial.begin(9600);
classUnit(input);
Serial.println("TEST");
Serial.println(units[0].building);
Serial.println(units[1].building);
Serial.println(units[0].unit[0]);
Serial.println(units[0].unit[1]);
}
void loop() {
}
void classUnit (String json) {
DynamicJsonDocument doc(30000);
deserializeJson(doc, json);
JsonObject root = doc.as<JsonObject>();
int count = 0;
int count2;
int count3;
for (JsonPair kv : root)
{
count2 = 0;
units[count].building = kv.key().c_str();
Serial.println(units[count].building);
for (JsonPair BUILDING_item : doc[units[count].building].as<JsonObject>()) {
count3 = 0;
units[count].unit[count2] = BUILDING_item.key().c_str();
Serial.println(units[count].unit[count2]);
for (JsonPair names_item : doc[units[count].building][units[count].unit[count2]]["names"].as<JsonObject>()) {
units[count].names[count2][count3] = names_item.key().c_str();
Serial.println(units[count].names[count2][count3]);
count3++;
}
count2++;
}
count++;
}
}
In my code, I am not able to correctly store the variables. Could someone help me?