SparkJson Encoding

I am testing SparkJson to see if it will work for my application, and I have hit a snag when I attempt to set String to a JsonObject. For example, the last line of the code listed below does not compile. The compiler states that the rvalue is ambiguous whenever I use a string variable in place of string defined in double quotes.

    String gnodeMessage = "";
    StaticJsonBuffer<1024> jsonBuffer;
    JsonObject& object = jsonBuffer.createObject();
    JsonObject& gnode = object.createNestedObject("gnode");
    gnode["version"] = "1.0"; // this line compiles, and testing shows it works
    gnode["message"] = gnodeMessage; // this line will not compile

I checked the ArduinoJson documentation, and I can’t spot any syntax errors.
Citing: https://bblanchon.github.io/ArduinoJson/example/string/
// You can set a String to a JsonObject or JsonArray:
// WARNING: the content of the String will be duplicated in the JsonBuffer.
root[“sensor”] = sensor;

Can anyone tell this SparkJson noob what I am doing wrong?

This should work:

gnode["message"] = gnodeMessage.c_str();

or, if you prefer:

gnode["message"] = (const char *) gnodeMessage;

The reason is that SparkJson doesn’t know about String, and String doesn’t know what format the left side of the expression wants, so you need to tell it explicitly.

1 Like

That makes perfect sense … thanks, I’ll give it a try.

Did his suggestion help you?

Yep, … worked like a charm, and I understand why. Thanks

1 Like