0

我想要来自 Esp8266 的这个输出 Json

我正在尝试使用另一个 json lib https://github.com/arduino-libraries/Arduino_JSON 这是比 arduinojson.org 轻量级的库

{
  "ID": "0785E7",
  "DEVICE": "SINGLE_RELAY",
  "Public_Name": "Demo Switch",
  "version": 1,
  "payloadVersion": 1,
  "SW": 1,
  "HW": 1,
  "CHIPSIZE": 1048576,
  "CHIPSPEED": 40000000,
  "CHIPMODE": 3,
  "capabilities": [
    {
      "interface": "Demo.Switch",
      "type": "ONOFF",
      "version": "1.0",
      "sync": true,
      "control_pin": 2,
      "MQTT_SWITCH_TOPIC": "on",
      "MQTT_SWITCH_SYNC": "sync"
    },
    {
      "interface": "Demo.Countdown",
      "type": "Countdown",
      "version": "1.0",
      "sync": true,
      "control_pin": 2,
      "MQTT_SWITCH_TOPIC": "s",
      "MQTT_SWITCH_SYNC": "x"
    },
    {
      "interface": "Demo.Schedule",
      "type": "Schedule",
      "version": "1.0",
      "sync": true,
      "control_pin": 2,
      "MQTT_SWITCH_TOPIC": "s",
      "MQTT_SWITCH_SYNC": "x"
    }
  ]
}

Arduino 功能

String Device_info()
{

 JSONVar json;
  json["ID"] = ESPID;
  json["DEVICE"] = DEVICE_TYPE;
  json["Public_Name"] = DEVICE_CHANNEL;
  json["version"] = relaystatus1;
  json["payloadVersion"] = RelayPIN;
  json["SW"] = userid;
  json["HW"] = Trigger;
  json["CHIPSIZE"] = rssi_str;
  json["CHIPSPEED"] = "1";;
  String jsonString = JSON.stringify(json);

}

我想在这个 Like Above json 中添加“功能”objectArray。

4

2 回答 2

3

createNestedObject()方法可用于在嵌套数组中创建对象capabilities

ArduinoJson助手建议使用以下代码:

const size_t capacity = 
    JSON_ARRAY_SIZE(3) + 3*JSON_OBJECT_SIZE(7) + JSON_OBJECT_SIZE(11);

DynamicJsonDocument doc(capacity);

doc["ID"] = "0785E7";
doc["DEVICE"] = "SINGLE_RELAY";
doc["Public_Name"] = "Demo Switch";
doc["version"] = 1;
doc["payloadVersion"] = 1;
doc["SW"] = 1;
doc["HW"] = 1;
doc["CHIPSIZE"] = 1048576;
doc["CHIPSPEED"] = 40000000;
doc["CHIPMODE"] = 3;

JsonArray capabilities = doc.createNestedArray("capabilities");

JsonObject capabilities_0 = capabilities.createNestedObject();
capabilities_0["interface"] = "Demo.Switch";
capabilities_0["type"] = "ONOFF";
capabilities_0["version"] = "1.0";
capabilities_0["sync"] = true;
capabilities_0["control_pin"] = 2;
capabilities_0["MQTT_SWITCH_TOPIC"] = "on";
capabilities_0["MQTT_SWITCH_SYNC"] = "sync";

JsonObject capabilities_1 = capabilities.createNestedObject();
capabilities_1["interface"] = "Demo.Countdown";
capabilities_1["type"] = "Countdown";
capabilities_1["version"] = "1.0";
capabilities_1["sync"] = true;
capabilities_1["control_pin"] = 2;
capabilities_1["MQTT_SWITCH_TOPIC"] = "s";
capabilities_1["MQTT_SWITCH_SYNC"] = "x";

JsonObject capabilities_2 = capabilities.createNestedObject();
capabilities_2["interface"] = "Demo.Schedule";
capabilities_2["type"] = "Schedule";
capabilities_2["version"] = "1.0";
capabilities_2["sync"] = true;
capabilities_2["control_pin"] = 2;
capabilities_2["MQTT_SWITCH_TOPIC"] = "s";
capabilities_2["MQTT_SWITCH_SYNC"] = "x";

serializeJson(doc, Serial);
于 2019-11-24T04:13:13.347 回答
2
  JSONVar supported;
  JSONVar Countdown;
  Countdown["interface"] = interface_countdown;
  Countdown["type"] = interface_countdown_type;
  Countdown["version"] = version_1;
  Countdown["retrievable"] = true;
  Countdown["MIN_COUNT"] = 1;
  Countdown["MAX_COUNT"] = COUNTDOWNCOUNT;
  Countdown["control_pin"] = RELAY_PIN;
  Countdown["MQTT_TOPIC"] = MQTT_COUNTDOWN_IN;
  Countdown["MQTT_SYNC"] = MQTT_COUNTDOWN_IN;


  JSONVar schedule;
  schedule["interface"] = interface_Schedule;
  schedule["type"] = interface_Schedule_type;
  schedule["version"] = version_1;
  schedule["retrievable"] = true;
  schedule["control_pin"] = RELAY_PIN;
  schedule["MIN_COUNT"] = 1;
  schedule["MAX_COUNT"] = SCHEDUECOUNT;
  schedule["MQTT_TOPIC"] = MQTT_TIMMER_IN;
  schedule["MQTT_SYNC"] = MQTT_TIMMER_IN;



  supported[0] = Countdown;
  supported[1] = schedule;





  JSONVar power_control;
  power_control["interface"] = interface_power;
  power_control["type"] = interface_power_type;
  power_control["version"] = version_1;
  power_control["retrievable"] = true;
  power_control["proactivelyReported"] = true;
  power_control["control_pin"] = RELAY_PIN;
  power_control["MQTT_SWITCH_TOPIC"] = MQTT_SWITCH_COMMAND_TOPIC1;
  power_control["MQTT_SWITCH_SYNC"] = MQTT_SWITCH_PUBLISH_TOPIC1;
  power_control["supported"] = supported;



  JSONVar capabilities;


  capabilities[0] = power_control;


  delay(200);

  JSONVar json;
  json["ID"] = ESPID;
  json["DEVICE"] = DEVICE_TYPE;
  json["MACADDRESS"] = String(WiFi.macAddress());
  json["Public_Name"] = DEVICE_CHANNEL;
  json["version"] = version_1;
  json["payloadVersion"] = payloadVersion;
  json["SW"] = SW;
  json["HW"] = HW;
  json["CHIPSIZE"] = (int)ESP.getFlashChipRealSize();
  json["CHIPSPEED"] = (int)ESP.getFlashChipSpeed();
  json["capabilities"] = capabilities;



  delay(200);

  String jsonString = JSON.stringify(json);
  Serial.println(jsonString);
于 2019-12-03T14:48:55.940 回答