-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Closed
Labels
Description
I have dug and dug and looked everywhere ... and I'm sure there HAS to be a clear example of how to properly convert a String to use with ArduinoJson ... but I just can not find one.
EVERYONE seems to want to just say "Don't use Strings!" ... but we HAVE TO as we already have a ton of code that can not be rewritten ... and we have the spare memory. So we THOUGHT we could simply convert the String to a char array using the standard functions ... but it simply refuses to work ... and we know we are doing something wrong.
String responseBodyString = "{"
"\"status\":\"success\","
"\"version\":\"1.0\","
"\"timeEpoch\":1480823123,"
"\"errorURL\":\"http://ws.website.net/errors\","
"\"actions\": ["
"{"
"\"action\":\"updateProperty\","
"\"property\":\"some.property.1\","
"\"value\":\"some value 1\""
"},"
"{"
"\"action\":\"updateProperty\","
"\"property\":\"some.property.2\","
"\"value\":\"some value 2\""
"},"
"{"
"\"action\":\"updateProperty\","
"\"property\":\"some.property.3\","
"\"value\":\"some value 3\""
"}"
"]"
"}";
char responseBody[] = "{"
"\"status\":\"success\","
"\"version\":\"1.0\","
"\"timeEpoch\":1480823123,"
"\"errorURL\":\"http://ws.wovyn.net/errors\","
"\"actions\": ["
"{"
"\"action\":\"updateProperty\","
"\"property\":\"some.property.1\","
"\"value\":\"some value 1\""
"},"
"{"
"\"action\":\"updateProperty\","
"\"property\":\"some.property.2\","
"\"value\":\"some value 2\""
"},"
"{"
"\"action\":\"updateProperty\","
"\"property\":\"some.property.3\","
"\"value\":\"some value 3\""
"}"
"]"
"}";
int lengthString = responseBodyString.length();
Serial.print(F("JSON String response length: ")); // always one char shorter than the char[]
Serial.println(lengthString);
int length = sizeof(responseBody);
Serial.print(F("JSON response length: ")); // always one char longer than the string?
Serial.println(length);
// Allocate a buffer to store contents of the file.
// std::unique_ptr<char[]> buf(new char[length]);
// We don't use String here because ArduinoJson library requires the input
// buffer to be mutable. If you don't use ArduinoJson, you may as well
// use configFile.readString instead.
// configFile.readBytes(buf.get(), size);
// attempt to convert the String to char[]
char buf[lengthString + 1];
responseBodyString.toCharArray(buf, lengthString);
buf[lengthString] = 0; // zero terminate? This was a guess ...
StaticJsonBuffer<500> jsonStringBuffer;
JsonObject& jsonString = jsonStringBuffer.parseObject(responseBodyString);
if (!jsonString.success()) {
Serial.println(F("Failed to parse responseBodyString")); // ALWAYS FAILS! ;-(
return false;
}
StaticJsonBuffer<500> jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(responseBody);
if (!json.success()) {
Serial.println(F("Failed to parse responseBody")); // ALWAYS WORKS! ;-(
return false;
}
I know there must be something simple we are missing, and not understanding about doing this conversion. We don't see any other error, except that the parse fails.
Thank you in advance ... this is driving us nuts!