简体   繁体   English

ESP32 从 HTTP GET 响应创建一个可访问的 JSON 数组

[英]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.我正在使用 Google Cloud Firestore,我正在创建一个 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.我可以设法从我的 firestore 获得响应,但我不知道如何创建一个可访问的 json 数组,以便我可以从此数组中获取特定信息。
This is my ESP32 Code:这是我的 ESP32 代码:

#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.转换为 JSON 数组非常容易。 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"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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