ArduinoJson.h 5.13.4的两种创建方式

方式1:适用于少量数据的快速格式化
方式2:适用于固定格式的快速格式化

方式 1

void callbackupdata() {
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& object = jsonBuffer.createObject();
  object["deviceID"] = deviceID;
  object["productID"] = serialnumber ;
  object.prettyPrintTo(Serial);

结果1

  {
09:20:43.148 ->   "deviceID": "LHJ-002",
09:20:43.148 ->   "productID": "31303146413030313904303130303036",
}

方式 2

#define ALINK_BODY_FORMAT         "{\"Device_ID\":\"laohuajia-003\",\"version\":\"1.0.0\",\"method\":\"thing.event.property.post\",\"params\":%s}"

void mqttIntervalPost()
{
  char param0[480];
  char param[480];
  char jsonBuf[512];
  j = sprintf(param0, "\"productID\":\"%s\"\r,", productID);
  j += sprintf(param0 + j, "\"CurrentTemperature\":%d\r,", CurrentTemperature);
  j += sprintf(param0 + j, "\"CurrentVoltage\":%d\r", CurrentVoltage);
  sprintf(param , "{\r%s}\r", param0);
  sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
  Serial.println(jsonBuf);
 }

结果2

{
"Device_ID":"laohuajia-003",
"version":"1.0.0",
"method":"thing.event.property.post",
"params":{
		"productID":"101FA001904010013",
		"CurrentTemperature":52,
		"CurrentVoltage":11
		}
}

你可能感兴趣的:(arduino)