I have a problem with JSON. I have a pointer into the JSON data, but in Void Loop the data is overwritten and my program can't execute with the JSON data. Only sometimes the JSON data is damaged and overwritten. I have also tried with StaticJsonDocument but it didn't compile. Don't what I did wrong. I am not very good at programming Arduino.
Can you help to move JSON data to a simple array? Please just edit the code below directly and upload here.
The JSON data just contains H_RESPITSEK=x; H_RESPITSEK=y; H_RESPITSEK=z; and so on. The length of the JSON is variates and therefore I need x,y,z in a simple array for the rest of the program.
A part of the code:
const char* RESPITSEKARR[1000]; // <= this must contain static data which is not overwritten.
void setup(){
if(H_RESPITSTART=="J"){
String serverPathData3 = serverName + "?klubid=" + KLUBID + "&hornres=J"; // set server and request data
Serial.print(" ");
Serial.println(serverPathData3); // print server path
http.begin(client, serverPathData3.c_str()); // begin request
httpResponseCode = http.GET(); // get data
if (httpResponseCode > 0) { // if http runs OK
Serial.print(" JSON HTTP Response code: "); // print http code
Serial.println(httpResponseCode);
String payload3 = http.getString(); // read http response
Serial.print(" ");
Serial.println(payload3); // print http response
//https://arduinojson.org/ --> Assistent menu, to generate json code without nested elements
//StaticJsonDocument<512> doc; // define static JSON example
DynamicJsonDocument doc(5000); // define dynamic JSON with beginning size
DeserializationError error = deserializeJson(doc, payload3); // parsen JSON data to doc
if (error) { // if parse error
Serial.print(" deserializeJson() failed: "); // print error
Serial.println(error.c_str());
return;
}
JsonArray nested = doc.as<JsonArray>(); // JSON array is nested (two dimensionel)
//NUMBER OF LINES IN JSON
int arrayLength3 = doc.size(); // size of doc equals number of array lines
respitlines= doc.size(); // put array lines to var
Serial.print(" JSON lines = "); // print number of array lines
Serial.println(arrayLength3);
int i = 0;
for (JsonObject item : nested) {
// * = is a pointer into the JSON file. JSON will be overwritten later
// DATA will be overwritten since item is a pointer
DATA[i][6] = item["H_RESPITSEK"];
RESPITSEKARR[i]=DATA[i][6];
Serial.print(" RESPITSEK ARR = ");
Serial.println(RESPITSEKARR[i]);
i++;
}
} // ends if for horn respit data
else {
Serial.print(" Error code: "); // else print http error code if wifi not connected
Serial.println(httpResponseCode);
}
http.end(); // close http connection
} //respit read in finished
}
void loop(){
some code here
}