【问题标题】:create struct from JSON object in c++ / Arduino在 c++ / Arduino 中从 JSON 对象创建结构
【发布时间】:2021-01-26 18:17:17
【问题描述】:

我的 Arduino 上的 SD 上有一个很大的 JSON 配置文件

"general": {
        "dhcp": true,
        "macAddress": [500,500,500,500,500,500],
        "destinationIP": [500,500,500,500],
        "serverHostname": null,
        "interval": 5,
        "commaDecimalSeperator": true,
        "baudrate": 115200
    },
    "networking": {
        "ipAddress": [500,500,500,500],
        "subnetMask": [500,500,500,500],
        "gateway": [500,500,500,500]
    },
    "data": {
        "csvHeader": "bla;bla;bla",
        "csvSeperator": ";",
        "filename": "data.csv"
    },
and so on...

有没有办法在 Arduino 中将 JSON 对象转换为结构或类似的东西?因为目前我的很多代码只是将 JSON 对象分配给具有相同名称的变量

我当前的代码:

struct General {
  bool dhcp;
  byte mac[6];
  int destinationIP[4];
  String serverHostname;
  int interval;
  bool commaDecimalSeperator;
  long baudrate;
};

void setup() {
  JsonObject general_1 = doc["general"];
  general.dhcp = general_1["dhcp"];
  general.interval = general_1["interval"];
  general.commaDecimalSeperator = general_1["commaDecimalSeperator"];
  general.baudrate = general_1["baudrate"];
}
...

我想要什么:

loadJSONToStructs(doc)
Serial.print(General.dhcp)

【问题讨论】:

    标签: c++ json object struct arduino


    【解决方案1】:

    ArduinoJson 库有一个漂亮的 online assistant tool 来帮助生成其中的一些内容,例如:

    // char* input;
    // size_t inputLength; (optional)
    
    StaticJsonDocument<768> doc;
    deserializeJson(doc, input, inputLength);
    
    JsonObject general = doc["general"];
    
    JsonArray general_macAddress = general["macAddress"];
    int general_macAddress_0 = general_macAddress[0]; // 227
    int general_macAddress_1 = general_macAddress[1]; // 197
    int general_macAddress_2 = general_macAddress[2]; // 182
    int general_macAddress_3 = general_macAddress[3]; // 211
    int general_macAddress_4 = general_macAddress[4]; // 1
    int general_macAddress_5 = general_macAddress[5]; // 201
    
    JsonArray general_destinationIP = general["destinationIP"];
    int general_destinationIP_0 = general_destinationIP[0]; // 10
    int general_destinationIP_1 = general_destinationIP[1]; // 0
    int general_destinationIP_2 = general_destinationIP[2]; // 0
    int general_destinationIP_3 = general_destinationIP[3]; // 2
    
    int general_interval = general["interval"]; // 5
    long general_baudrate = general["baudrate"]; // 115200
    
    JsonObject networking = doc["networking"];
    
    JsonArray networking_ipAddress = networking["ipAddress"];
    int networking_ipAddress_0 = networking_ipAddress[0]; // 10
    int networking_ipAddress_1 = networking_ipAddress[1]; // 0
    int networking_ipAddress_2 = networking_ipAddress[2]; // 0
    int networking_ipAddress_3 = networking_ipAddress[3]; // 3
    
    JsonArray networking_subnetMask = networking["subnetMask"];
    int networking_subnetMask_0 = networking_subnetMask[0]; // 255
    int networking_subnetMask_1 = networking_subnetMask[1]; // 0
    int networking_subnetMask_2 = networking_subnetMask[2]; // 0
    int networking_subnetMask_3 = networking_subnetMask[3]; // 0
    
    JsonArray networking_gateway = networking["gateway"];
    int networking_gateway_0 = networking_gateway[0]; // 10
    int networking_gateway_1 = networking_gateway[1]; // 0
    int networking_gateway_2 = networking_gateway[2]; // 0
    int networking_gateway_3 = networking_gateway[3]; // 1
    
    JsonObject data = doc["data"];
    const char* data_csvHeader = data["csvHeader"]; // "bla;bla;bla"
    const char* data_csvSeperator = data["csvSeperator"]; // ";"
    const char* data_filename = data["filename"]; // "data.csv"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 2012-12-14
      • 2019-03-23
      相关资源
      最近更新 更多