ArduinoJson 6 的 ESPAsyncWebServer JSON 响应示例

ESPAsyncWebserver 页面提供了一个使用 ArduinoJson 生成基本 JSON 响应的示例:

example-2.cpp
#include "ArduinoJson.h"

AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["heap"] = ESP.getFreeHeap();
root["ssid"] = WiFi.SSID();
root.printTo(*response);
request->send(response);

然而,该示例是为 ArduinoJson 5.x 制作的,而大多数用户想使用更新的 ArduinoJson 6.x。

ArduinoJson 6.x 最小 ESPAsyncWebserver 示例:

example-1.cpp
DynamicJsonDocument json(1024);
json["status"] = "ok";
json["ssid"] = WiFi.SSID();
json["ip"] = WiFi.localIP();
serializeJson(json, *response);
request->send(response);

Check out similar posts by category: Embedded, ESP8266/ESP32, PlatformIO