I'm parsing JSON in my Arduino sketch and having issues. I think it has to do with escaping characters but not sure. I'm calling the web service from Adafruit QT PY using Python without any issues.
if test with the following text it works as expected String wsResponse = "{\"ResponseCode\":1,\"ResponseMessage\":\"Success\",\"LogID\":13106}";
There are backslashes in the text, not sure why will display
I don't think it's an escaping characters issue, anyway does "error.code" returned something Like "DeserializationError::Ok" or what else (see Deserialization error)?
Ok, so the Json string looks ok, and no error on deserialize.
But you didn't use my code (and missed "wsResponseCode" value).
Please change the code to include my modifications and paste here the whole and only serial monitor output.
Doc, I reread your post but I cannot get your code to compile. I receive the following error on line, Serial.println(jsonBuffer["ResponseCode"]);
GetHelp:599:60: error: call of overloaded 'println(ArduinoJson6194_F1::enable_if<true, ArduinoJson6194_F1::MemberProxy<ArduinoJson6194_F1::JsonDocument&, const char*> >::type)' is ambiguous
Serial.println(jsonBuffer["ResponseCode"]);
I thought I found the issue but still no luck. I was serializing my content to JSON twice so that is why I was seeing the backslashes. However I updated the code to only serialize once and still have the issue. I called my service via Postman and see the Content-Type is set to JSON and the body looks like JSON now. Not sure what to do
You're right, my fault. The "jsonBuffer" variable is a "StaticJsonDocument" type, not directly handled by Serial.print() so you should try explicitly cast it. I don't have now access to my development PC at home to give it a try, but this could/should work:
The Serial.print(ResponseCode); is just to get what the deserialized json buffer contains, then converted to an int: if it's empty, the wsResponseCode int value will be obviously zero, and something went wrong with the deserializeJson() function (but if "error" is not returned, I really can't understand why).
Please compile and run this code and post here the serial output.
PS: if you need/want to directly use String variable, the ArduinoJson documentation says you can do it, but should define this symbol first:
First thank you, turns out I had a sequence of errors that are now resolved. First I double serialized my JSON string which led to the backslashes I mentioned in my original post. Then when I fixed my web service to only serialize the JSON once it started returning in camel case which was causing the last error we were discussing. So all is good now, except I cannot figure out how not to use a String to receive clientHTTP.responseBody();
Try something like this to get on a char array the first row of received data (my Arduino develop PC is still out of order, I suppose the CPU has gone , so I can't make sure it works..):
const int BUFFERSIZE = 80; // Set the highest size here + 1
char buf[BUFFERSIZE];
int ptr = 0;
char c;
while (clientHTTP.available() && ptr < BUFFERSIZE) {
c = clientHTTP.read();
if (c == '\n') // End of line
break;
if (c != '\r') // Ignore CRs
{
buf[ptr++] = c;
buf[ptr] = '\0';
}
}
// Here you have "buf" filled with the first response line (I hope it's the json data you need)