Hello Team,
I'm trying to parse the JSON array mentioned below in this topic.
I want to extract all the float Values of the Key "Rating".
[{"Rating":5},{"Rating":0},{"Rating":5},{"Rating":4.5},{"Rating":5},{"Rating":3},{"Rating":2.5},{"Rating":3},{"Rating":5},{"Rating":4.5}]
Please suggest some way to achieve the target.
Thanks in advance.
J-M-L
2
using a JSON library is the easiest way. parsing would not seem very complicated
see https://arduinojson.org or Arduino_JSON - Arduino Reference
Jackson,
Thanks for the response.
I tried JSON Library. Particularly Array of JSON is a bit challenging for me.
J-M-L
4
then do it manually 
something like this (assuming you run on an ESP and not a small arduino)
char json[] = "[{\"Rating\":5},{\"Rating\":0},{\"Rating\":5},{\"Rating\":4.5},{\"Rating\":5},{\"Rating\":3},{\"Rating\":2.5},{\"Rating\":3},{\"Rating\":5},{\"Rating\":4.5}]";
void parseJson(char * jsonBuffer) {
char *ptr = strchr(jsonBuffer, ':');
char * endPtr;
while (ptr && *ptr) {
float f = strtof(ptr + 1, &endPtr);
if (endPtr == ptr + 1) {
Serial.print("Error at "); Serial.println(ptr);
} else {
Serial.print("Found rating :"); Serial.println(f, 3);
}
ptr = strchr(ptr+1, ':');
}
}
void setup() {
Serial.begin(115200); Serial.println();
parseJson(json);
}
void loop() {}
(untested just typed here)
Appreciate your response.
The below snippet of code worked for me.:
CurrData = [{"Rating":5},{"Rating":0},{"Rating":5},{"Rating":4.5},{"Rating":5},{"Rating":3},{"Rating":2.5},{"Rating":3},{"Rating":5},{"Rating":4.5}]
deserializeJson(doc, CurrData);
for (JsonObject item : doc.as<JsonArray>()) {
float Rating = item["Rating"];
Serial.println(Rating);
}
J-M-L
6
good
or you can do something like this to
#include <ArduinoJson.h>
char json[] = "[{\"Rating\":5},{\"Rating\":0},{\"Rating\":5},{\"Rating\":4.5},{\"Rating\":5},{\"Rating\":3},{\"Rating\":2.5},{\"Rating\":3},{\"Rating\":5},{\"Rating\":4.5}]";
void parseJson(char * jsonBuffer) {
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, jsonBuffer);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
for (size_t i = 0; i < doc.size(); i++) {
Serial.print(i);
Serial.write('\t');
float value = doc[i]["Rating"];
Serial.println(value);
}
}
void setup() {
Serial.begin(115200); Serial.println();
parseJson(json);
}
void loop() {}
Yes. Even this is working.
Actually, your first response helped me to get the solution.
ArduinoJson Assistant --> Made a snippet of code as per my requirement and It worked in the first shot.
Thank you so much.
system
Closed
8
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.