Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

create struct from JSON object in c++ / Arduino

I have a big JSON config file on an SD on my Arduino

"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...

is there a way of turning JSON objects into structs or something like that in Arduino? Because at the moment a lot of my code is just assigning JSON objects to variables with the same name

My current code:

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"];
}
...

What I would like:

loadJSONToStructs(doc)
Serial.print(General.dhcp)
question from:https://stackoverflow.com/questions/65906971/create-struct-from-json-object-in-c-arduino

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The ArduinoJson library has a nifty online assistant tool to help generate some of that, e.g.:

// 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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...