#include <ArduinoJson.h>
struct clientData {
char value[8];
char humidity[8];
};
clientData myClientData; // Define an instance of the clientData struct
void parseJsonResponse(const String& jsonResponse) {
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, jsonResponse);
if (!error) {
JsonObject data = doc["data"];
// Retrieve the value of "ahoj" key and convert it to a string
int ahojValue = data["ahoj"];
itoa(ahojValue, myClientData.value, 10);
Serial.print("Value: ");
Serial.println(myClientData.value);
// Loop through each key-value pair in the 'data' object
for (JsonPair kv : data) {
float device_value = kv.value();
Serial.print("Device ID: ");
Serial.print(kv.key().c_str());
Serial.print(", Value: ");
Serial.println(device_value);
}
} else {
Serial.println("Failed to parse JSON");
}
}
void setup() {
Serial.begin(115200);
delay(1000);
// Simulating an API response (replace this with your actual API response)
String apiResponse = "{\"data\": {\"ahoj\": 1, \"device1\": 10.5, \"device2\": 20.7}}";
parseJsonResponse(apiResponse);
}
void loop() {
// Your code here
}
Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1