简体   繁体   中英

How to save JSON parsing values to an array in ArduinoJson

Arduino users, help pls, Hi guys: For example, I have a JSON document for parsing ArduinoJson:

{
  "id": [
    1,
    7,
    32,
    9656
  ]
}

I need to save the id values so that they look like:

ids[0] = 1,

ids[1] = 7,

ids[2] = 32and so on.

Now I'm doing like this (I know that it's fundamentally wrong, but I don't know how else):

I'll skip the step of request by the link

char json[500];
  getids.toCharArray(json, 500);
  Serial.println(json);
  StaticJsonDocument <500> doc;
  DeserializationError err = deserializeJson(doc, json);

Then I just try to equal values to the array:

int ids[16] = {doc["id"]};

But when I output ids[0], I get 0

Also, the problem is that I do not know in advance how many elements will be in this array - this file is regularly edited in the phone app and my JSON document can change the number of ids. Now there are 4 of them, but this is as an example, because their number is unknown, which makes it necessary to make a dynamic array, and not set it yourself, as I set [16]

ArduinoJson assistant offers this option:

JsonArray id = doc["id"];
int id_0 = id[0]; // 1
int id_1 = id[1]; // 7
int id_2 = id[2]; // 1337
int id_3 = id[3]; // 9656

But this does not fit, because, again, I do not know the number of elements in advance

This is my first question on Stackoverflow, so I'm sorry if I asked it incorrectly

It's may just use like:

JsonArray ids = doc["id"]

and use it inside functions like ids[1], ids[2] and other

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM