ESP-IDF HTTP webserver 最小 ArduinoJson 序列化示例
esp_http_handler.cpp
static const httpd_uri_t valueHandler = {
.uri = "/api/value",
.method = HTTP_GET,
.handler = [](httpd_req_t *req) {
httpd_resp_set_type(req, "application/json");
// 创建 JSON 文档
DynamicJsonDocument json(1024);
json["value"] = 1.0;
// 将 JSON 序列化为字符串
std::string buf;
serializeJson(json, buf);
// 发送响应
httpd_resp_send(req, buf.c_str(), buf.length());
return ESP_OK;
}
};要将 ArduinoJson 添加到 PlatformIO,将以下 lib_deps 添加到 platformio.ini:
platformio.ini
lib_deps =
ArduinoJSON@6.17.2Check out similar posts by category:
Arduino, ESP8266/ESP32
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow