简体   繁体   中英

ESP32 Create a accessible JSON Array from HTTP GET Response

I'm working with Google Cloud Firestore, and I'm creating a HTTP Get reponse to my file inside firestore. I could manage to get the response from my firestore, but I don't know how I would be able to create a accessible json array so I can get, specific info from this Array.
This is my ESP32 Code:

#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <WiFi.h>

const char* ssid = "ds2.4G";
const char* password = "sd";

void setup() {
Serial.begin(115200);

WiFi.begin(ssid, password);

Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.print(".");
}
}

void loop() {
if (WiFi.status() == WL_CONNECTED){

HTTPClient http;

http.begin("https://us-central1-home-f-d.f.net/app/api/read/f");
//http.addHeader("Content-Type", "application/json"); //Isso é para mandar dados
int httpCode = http.GET();   //Isso é para get request

if (httpCode > 0){
  String payload = http.getString();          //ISSO É PARA GET REQUEST
  Serial.println(httpCode);
  Serial.println(payload);
}

http.end();  
}else{
Serial.println("Chekar Conexão co WiFi");
}

delay(10000);

}

This is the reponse i get back from my request:

Connecting to WiFi
22:29:54.626 -> .....200
22:30:02.207 -> 
{"name":"Pedro","quantidade":20,"led3":false,"led1":false,"led2":false,"sensores":"sensor"}

Since I found how to do it. I will be answering my own post. It's pretty easy to transform into a JSON array. Here is the code:

char json[500];
payload.toCharArray(json, 500);
StaticJsonDocument<1024> doc;
deserializeJson(doc, json);

For getting a specific document value, you just need to do this:

<type> <name of your string> = doc["the name of the doc you want to gather info"]
int quant = doc["quantidade"];
String led1 = doc["led1"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM