Hello to all the Arduino community,
I am a French student in the last year of high school and I have to realize a project that displays the next train times. So, I decided to use an Arduino (ESP-8266) and an I2c screen.
For the moment, I want to get from a website data in JSON (with an API, it works).
But, when I want for example to get the train name it doesn't work all the time, it depends on the syntax of JSON.
Here is an illustration of my problem:
First example :
There are only "{" and "}" at the beginning and the end of the file. It works with my code.
Capture 1
Second example : There are "[" and "]" at the beginning and the end of the json file, there are still the "{" and "}". I want to get the "shortName", "vehicleName"... But it does not work...
Capture 2
Here is my code to grab the information from the website, it only works with the first example :
int id = root["id"];
const char* shortName = root["shortName"];
const char* vehicleName = root["vehicleName"];
const char* lineDirection = root["lineDirection"];
const char* time = root["time"];
So, does anyone know how to edit the code in order that it will work with the "[", "{", "}" "]" ?
Thank you very much for reading my and for your answers !
Lucas
What programming language is your code snippet written in ?
My snippet is probably the arduino programming language which should be "C".
Here is the full code if you want :
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "XX";
const char* password = "XX";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("http://perfeito.fr/test.json");
int httpCode = http.GET();
if (httpCode > 0) {
const size_t bufferSize = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(8) + 370;
DynamicJsonBuffer jsonBuffer(bufferSize);
JsonObject& root = jsonBuffer.parseObject(http.getString());
const char* shortName = root["shortName"];
const char* vehicleName = root["vehicleName"];
const char* lineDirection = root["lineDirection"];
const char* time = root["time"];
Serial.print("Nom ligne:");
Serial.println(shortName);
Serial.print("Nom RER:");
Serial.println(vehicleName);
Serial.print("Direction:");
Serial.println(lineDirection);
Serial.print("Temps:");
Serial.println(time);
}
http.end();
}
delay(120000);
}
You probably want something like:
const char* shortName = root[0]["shortName"];
But you may want to use:
JsonDocument::is<T>()
to determine the type of the JSON object in advance, so you can then index one or more elements properly.
Read this page and the sub-links for more information.
My snippet is probably the arduino programming language which should be "C".
Here is the full code if you want :
The full code makes it clearer what you are doing which is why posters are asked to provide the whole program and not just a snippet
Thank you for your answers guys, i will try your solutions and tell you if it works!
UKHelibob, sorry for that now I know!