簡體   English   中英

ESP32 從 HTTP GET 響應創建一個可訪問的 JSON 數組

[英]ESP32 Create a accessible JSON Array from HTTP GET Response

我正在使用 Google Cloud Firestore,我正在創建一個 HTTP Get reponse to my file inside firestore。 我可以設法從我的 firestore 獲得響應,但我不知道如何創建一個可訪問的 json 數組,以便我可以從此數組中獲取特定信息。
這是我的 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);

}

這是我從我的請求中得到的回復:

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

因為我找到了怎么做。 我將回答我自己的帖子。 轉換為 JSON 數組非常容易。 這是代碼:

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

要獲取特定的文檔值,您只需要執行以下操作:

<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