Hi!
I'm working with an ESP12-F, on getting a payload from an API.
The string that comes when requested looks like the following (which gets printed on the serial monitor):
{"replies":["Pa_1264354_09th9845-rm32-95we-hny0-7u3fv7g4bhc_1_0_EMPLOYEE_"]}
But I am supposed to print it in the following way (to split by the underscore):
{"replies"
:["Pa
1264354
09th9845-rm32-95we-hny0-7u3fv7g4bhc
1
0
EMPLOYEE
"]}
I gathered from websites that I could use Json.parse to do so, but I could not get the required outcome.
Below is the code that I used.
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Arduino_JSON.h>
const char* ssid = "ssid";//WiFi Network Name
const char* password = "1234";// WiFi password
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi");
}
Serial.print("Connected to WiFi");
}
void loop() {
if(WiFi.status()== WL_CONNECTED){
String path = "URL provided";
String jsonBuffer = httpGETRequest(path.c_str());
Serial.println(jsonBuffer);
JSONVar jsonObject = JSON.parse(jsonBuffer);
if (JSON.typeof(jsonObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
Serial.print("JSON object = ");
Serial.println(jsonObject);
}
else {
Serial.println("WiFi Disconnected");
}
}
String httpGETRequest(const char* server) {
WiFiClient client;
HTTPClient http;
http.begin(client, server);
int httpCode = http.GET();
String payload = "{}";
if (httpCode>0) {
Serial.print("HTTP Code: ");
Serial.println(httpCode);
payload = http.getString();
}
else {
Serial.print("Error!");
}
// Free resources
http.end();
return payload;
}
Really appreciate if someone could point out what I have done wrong what I am to do to get the required output.
Thanks in advance!
Shainy Fonseka