Hi All,
I’m a little rusty, apologies to whom find this an easy question.
My goal; parse a JSON received via Particle.function and call a function based on the value of the key named “type”.
Example JSON: {"event":{"id":444,"attributes":{},"deviceId":1,"type":"connected"}}
What I have as code:
int handleJsonCommand(String arg) {
// Parse incoming JSON
JSONValue root = JSONValue::parseCopy(arg);
if (!root.isObject()) return -1;
// First: iterate root to find "event"
JSONValue eventObj;
{
JSONObjectIterator it(root);
while (it.next()) {
if (it.name() == "event") {
eventObj = it.value();
}
}
}
if (!eventObj.isObject()) return -2;
// Now iterate "event" object to find "type"
String typeStr = "";
JSONObjectIterator itEvent(eventObj);
while (itEvent.next()) {
if (itEvent.name() == "type") {
if (itEvent.value().isString()) {
typeStr = itEvent.value().toString();
}
}
}
if (typeStr.length() == 0) return -3;
Particle.publish("eventType", typeStr, PRIVATE);
return 1;
}
The error I receive on build:
no match for 'operator=' (operand types are 'String' and 'spark::JSONString')
Pointing to this line: typeStr = itEvent.value().toString();
Any command is appreciated that achieves the goal of extracting the value out of key “event”.