首页 > 解决方案 > 在序列化 JSON 的末尾获取一个 INT

问题描述

所以我正在更新这个,因为我已经得到了 JSON OUT 但是 JSON 后面跟着一个 INT

{“函数”:“时间报告”,“值”:545028}41

  #include <ArduinoJson.h>

char owner[] = "";
const int capacity = JSON_OBJECT_SIZE(2);
DynamicJsonDocument doc(capacity);


void setup() {

Serial.begin(115200);


}

void loop() {
  doc["function"]="timereport";
  doc["values"]=millis();
  Serial.println(serializeJson(doc, Serial));
  delay(5000);
}

第二次尝试:(帖子最后仍然有INT)

char json_string[256];  

http.begin("http://192.168.1.103:2000/hydroapi"); //Specify destination for HTTP request
  http.addHeader("Content-Type","application/json");
  doc["function"]="timereport";
  doc["values"]=millis();

  serializeJson(doc, json_string);
  int httpResponseCode = http.POST(json_string); //Send the actual POST request
  http.end();

标签: jsonarduinoesp8266esp32

解决方案


调用serializeJson将 json 打印到Serial(因为这是您传入的流)并返回打印的字节数,然后您将使用Serial.println. 删除调用以Serial.println避免打印字节数,只需调用serializeJson(doc, Serial);


推荐阅读