Hi guys, I'm trying to parse a json response from my web api:
This is a chunk, here I send the GET request, and I capture the response from the server:
if (client.connect(server, 80)) {
//Send the HTTP Request
Serial.println("Fetching Data...");
Serial.println();
client.println("GET "+path+" HTTP/1.1");
client.println("Host: localhost");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
//Capture the response
while (!rcvResponse){
if (client.available()){
rcvResponse=true;
while (client.available()){
char c = client.read();
http_response += c;
}
}
}
client.stop();
Serial.println("Website response STRING: ");
Serial.println(http_response);
Serial.println();
This is the output in JSON:
Website response STRING:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 195
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 24 Nov 2014 14:33:11 GMT
Connection: close{"Success":true,"ErrorDescription":null,"ErrorCode":null,"Data":{"DaysList":[{"EventId":2,"DayId":5,"NumberOfVisitors":0,"Done":false},{"EventId":2,"DayId":6,"NumberOfVisitors":0,"Done":false}]}}
Then I try to parse the response, in this way:
char httpJSON[http_response.length()+1];
http_response.toCharArray(httpJSON, http_response.length()+1);
Serial.println("Website response CHAR-ARRAY: ");
Serial.write(httpJSON);
Serial.println();
aJsonObject* root = aJson.parse(httpJSON);
if (root != NULL) {
Serial.println("OK: JSON Object obtained");
}
else{
Serial.println("ERROR: Unable to get the root.");
}
And then, the output:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 195
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 24 Nov 2014 14:33:11 GMT
Connection: close{"Success":true,"ErrorDescription":null,"ErrorCode":null,"Data":{"DaysList":[{"EventId":2,"DayI´×þûHTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 195
Content-Typ
ERROR: Unable to get the root.
As you can see, when I convert the string into a array of chars, I obtain an incomplete output, with strange chars...somebody can help me?