'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
Solution 1:[1]
It's may just use like:
JsonArray ids = doc["id"]
and use it inside functions like ids[1], ids[2] and other
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ????? ?????? |