Mar 11, 2020

IoT Particle: Parsing JSON into array of objects

suppose your JSON is as follows
String scheduleJson = "[{\"hour\":1.5, \"isFlash\": true}, {\"hour\":2, \"isFlash\": false}, {\"hour\":7.30, \"isFlash\": true}, {\"hour\":8, \"isFlash\": false}]";
we create a class representation of above JSON
class Schedule{
    public:
        float hour;
        bool isFlash;
};
From Libraries menu in Particle web IDE add reference to JsonParserGeneratorRK library
Delcare a JsonParser object as follows
JsonParser parser;
then within setup function we need to add JSON string to the parser.
void setup() {
    parser.clear();
    parser.addString(scheduleJson);
}
then we can use following snippet to parse the json

all code for sketch:

No comments:

Post a Comment

Be the first to comment on this post.