I want to parse JSON response from a GET Request. I do this usually by using a String variable to store the response and later parse this string using ArduinoJSON library like shown below:
#include <Arduino.h>
#include "OTA.h"
#include "Credentials.h"
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char url[] = "http://date.jsontest.com/";
HTTPClient getClient;
void getRequest(String &payload)
{
getClient.begin(url);
int httpCode = getClient.GET();
if (httpCode > 0)
{
payload = getClient.getString();
// Serial.print("HTTP Status: ");
// Serial.println(httpCode);
}
else
{
Serial.println("Error on HTTP request");
}
getClient.end();
}
void parseResponse(String &payload)
{
StaticJsonDocument<128> doc;
DeserializationError error = deserializeJson(doc, payload);
if (error)
{
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
const char *time = doc["time"]; // "03:53:25 AM"
// unsigned long *epoch = doc["milliseconds_since_epoch"]; // 1362196405309
const char *date = doc["date"]; // "03-02-2013"
Serial.println(date);
}
void setup()
{
setupOTA("ESP-N2", mySSID, myPASSWORD);
Serial.begin(115200);
}
void loop()
{
ArduinoOTA.handle();
unsigned long newTime = millis();
static unsigned long oldTime = 0;
String payload;
if (newTime - oldTime >= 5000)
{
oldTime = newTime;
getRequest(payload);
parseResponse(payload);
Serial.println();
}
}
I want to avoid using String and ArduinoJSON offician reference page mentions that it's possible to deserialize directly from input Stream e.g. wifiClient, ethernetclient or serial but I couldn't find any example showing it's usage. So I am curious to learn how to use it.
From my basic understanding of a Stream, I suppose I need define a buffer or memory location that will hold the incoming bytes and using the address of this buffer need to read data bit by bit. So do I need to do something like this:
char buffer[]= httpClient.getStream();
and then pass this buffer as input to deserialize function? or have I gotten it completely wrong?