Save JSON values in array

In below code I read in a json file. DATA is pointer into this JSON file. To secure the data from overwriting later in code I save the values to H_XXXXX variables. Now my problem is that DATA[i][6] = item["H_RESPITSEK"] actually shall be stored in an array since this value has different values for each i. But how can I save the this data in a secure array that will not be overwritten?

      //NUMBER OF LINES IN JSON
      int arrayLength = 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(arrayLength);

      //PRINT EACH LINE IN JSON
      int i = 0;
      for (JsonObject item : nested) {
        // * = is a pointer into the JSON file. JSON will be overwritten later
        const char* SERIE = item["H_SERIE"];                    // serie data
        const char* LOEBNR = item["H_LOEBNR"];                  // etc...
        const char* ANTALBAADE = item["H_ANTALBAADE"];
        const char* TIDMLSTART = item["H_TIDMLSTART"];
        const char* ANTALSTART = item["H_ANTALSTART"];
        const char* RESPITSTART = item["H_RESPITSTART"];
        const char* RESPITSEK = item["H_RESPITSEK"];
        const char* RESPITDIFF = item["H_RESPITDIFF"];
        const char* DATO = item["H_DATO"];
        const char* STARTTID = item["H_STARTTID"];
        const char* AAR = item["H_AAR"];
        const char* SERIEID = item["H_SERIEID"];
        // DATA will be overwritten since item is a pointer
        DATA[i][0] = item["H_SERIE"];                           // DATA array with items from each line.
        DATA[i][1] = item["H_LOEBNR"];
        DATA[i][2] = item["H_ANTALBAADE"];
        DATA[i][3] = item["H_TIDMLSTART"];
        DATA[i][4] = item["H_ANTALSTART"];
        DATA[i][5] = item["H_RESPITSTART"];
        DATA[i][6] = item["H_RESPITSEK"];
        DATA[i][7] = item["H_RESPITDIFF"];
        DATA[i][8] = item["H_DATO"];
        DATA[i][9] = item["H_STARTTID"];
        DATA[i][10] = item["H_AAR"];
        DATA[i][11] = item["H_SERIEID"];
        Serial.print("  Serie arr = ");                         // print data values
        Serial.println(DATA[i][0]);
        Serial.print("  Loebnr arr = ");
        Serial.println(DATA[i][1]);
        Serial.print("  Antal baade arr = ");
        Serial.println(DATA[i][2]);
        Serial.print("  Tidmlstart arr = ");
        Serial.println(DATA[i][3]);
        Serial.print("  Respitstart arr = ");
        Serial.println(DATA[i][5]);
        Serial.print("  Respitsek arr = ");
        Serial.println(DATA[i][6]);
        Serial.print("  Dato arr = ");
        Serial.println(DATA[i][8]);
        Serial.print("  Starttid arr = ");
        Serial.println(DATA[i][9]);
        Serial.print("  Aar arr = ");
        Serial.println(DATA[i][10]);
        Serial.print("  Serieid arr = ");
        Serial.println(DATA[i][11]);
        i++;
      } 

      // secure data for the run in static variables
      H_SERIE = DATA[0][0];
      H_LOEBNR = DATA[0][1];
      H_ANTALBAADE = DATA[0][2];
      H_TIDMLSTART = DATA[0][3];
      H_ANTALSTART = DATA[0][4];
      H_RESPITSTART = DATA[0][5];
      H_RESPITSEK = DATA[0][6];
      H_RESPITDIFF = DATA[0][7];
      H_DATO = DATA[0][8];
      H_STARTTID = DATA[0][9];
      H_AAR = DATA[0][10];
      H_SERIEID = DATA[0][11];

Create a two-dimensional array of characters; the second dimension should be big enough to hold the longest item["H_RESPITSEK"] plus one character.

char respitsek[20][40]; // array of 20 elements each having a maximal text length of 39 characters.

Use strcpy or strncpy to copy RESPITSEK to one of the elements of respitsek; you will have to keep track which element is free.

Thanks. I could do it with a simple one dimentional array since it is just a row of numbers.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.