Hello,
I'm using ArduinoJson to help parse the char array I create after sending a GET request for my hue lighting system.
Assume that the GET request is sent and the correct info is returned.
This is the sequence:
- I send a get request to the bridge (hue lighting system), and it returns the json info. (this works)
- I filter through them, storing only the section of the response that I want in json[].(this works).
- I check the array by printing out all the chars from json[].(this works)
- I use ArduinoJson to parse the char array. (not working as intended)
- For some reason that I can't figure out, it doesn't work using json[] which is my intention.
- but when I use json2[], it works. json2[] is a copy+paste of the serial monitor results in #3.
is there something wrong in the way that I am storing or handling json[] that treats it differently from json2[]?
I get no compiler errors, just "parseObject() failed"
note: i'm just looking for help with this parsing issue and why one works and the other does despite what seems like the same data. I'd appreciate the focus staying there and not on my likely-unorthodox or poor coding
Best,
//This is where the get request was just sent.
while(client.connected())
{
while(client.available() && endBrace == false)
{
char c = client.read();
if(readingJson == true)
{
if (c == '\"' ) // if quotes, add a slash then quotes
{
json[index] = '\\'; // insert slash
index++;
json[index] = c; // Store it
index++;
}
else if(c == '\}') // if end bracket, add bracket and then quotes and break loop
{
json[index] = c; // Store it
index++;
json[index] = '\"';
index++;
endBrace = true;
}
else// if no special characters, add to array
{
json[index] = c; // Store it
index++;
}
}
if(c == '\{' && countBrace == 1) // second bracket, add quotes
{
json[index] = '\"';
index++;
json[index] = c; // Store it
index++;
readingJson = true;
countBrace == 2;
}
if(c == '\{' && countBrace == 0) // first brace detected, skipped
{
countBrace = 1;
}
}
}
//checking index. this works.
Serial.println(" ");
Serial.println("index is ");
Serial.println(index);
Serial.println(" ");
// checking char stored. this works
Serial.println(" ");
Serial.println("json READ ");
Serial.println(" ");
for (int i = 0; i < index; i++)
{
Serial.print(json[i]);
}
Serial.println(" ");
// json2 is a fixed replica of the results printed from json[] above.
//char json2[] = "{\"on\":true,\"bri\":207,\"hue\":13234,\"sat\":208,\"effect\":\"none\",\"xy\":[0.5090,0.4149],\"ct\":459,\"alert\":\"none\",\"colormode\":\"xy\",\"reachable\":true}";
// Arduino Json
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json); // works with json2, not with json
if (!root.success())
{
Serial.println("parseObject() failed");
return;
}
int bri = root["bri"]; // works with json2, not with json
int hue = root["hue"]; // works with json2, not with json
int sat = root["sat"]; // works with json2, not with json
Serial.println(bri); // works with json2, not with json
Serial.println(hue); // works with json2, not with json
Serial.println(sat); // works with json2, not with json