Greetings,
I have a problem with parsing a json recived from from a get request..
I'm not sure if i'm doing it right.. maybe you can help me fixing it.
http://192.168.1.15/?j={test:true}} this works, but not really a valid json
http://192.168.1.15/?j={"test":"true"}} this doesn't work
the problem is that it doesn't parse the string if it's url encoded
I only get the first line of the request, that with the GET request.
I skip the first 8 chars.
{%22test%22:true}}
this is the char array that it's stored into the array;
and the code
#include "WiFiEsp.h"
[wifi stuff]
#include <ArduinoJson.h>
#define json_len 200
char json[json_len];
char json_response[json_len];
int json_index = 0;
int json_response_index = 0;
void setup()
{
[wifi stuff]
}
void loop()
{
WiFiEspClient client = server.available();
if (client) {
boolean currentLineIsBlank = false;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if(request_line_count == 0 && request_char_count == 5)
{
if(c == 'f')
{
skip = true;
}
}
if(request_line_count == 0)
{
request_char_count++;
if(request_char_count > 8)
{
json[json_index++] = c;
json[json_index] = '\0';
}
}
if (c == '\n' && currentLineIsBlank) {
parseRequest(client);
break;
}
if (c == '\n') {
currentLineIsBlank = true;
request_line_count++;
request_char_count = 0;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(10);
client.stop();
}
}
void parseRequest(WiFiEspClient client)
{
StaticJsonBuffer<json_len> jsonBuffer;
request_line_count = 0;
request_char_count = 0;
client.print(
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n"
"\r\n");
if(skip == true)
{
skip = false;
client.print(".");
}
else
{
Serial.println(json_index);
json[json_index - 10] = '\0';
Serial.println(json);
JsonObject& root = jsonBuffer.parse(json);
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
else
{
Serial.println("parseObject() successful");
Serial.println("decodat:");
root.prettyPrintTo(Serial);
client.print(json);
}
json_index = 0;
json[json_index] = '\0';
}