If array convert to json?

Sorry ,
i want to know convert array to json


char MyArray[3][3];
MyArray[0][0] = "1"; // id
MyArray[0][1] = "bat";  //name
MyArray[0][2] = "12";  //old
MyArray[0][3] = "16/05/57"; // register
MyArray[1][0] = "2"; // id
MyArray[1][1] = "john"; //name
MyArray[1][2] = "22"; //old
MyArray[1][3] = "16/03/57"; // register

MyArray[2][0] = "3"; // id
MyArray[2][1] = "Sala";  //name
MyArray[2][2] = "26";  //old
MyArray[2][3] = "16/01/57"; // register
MyArray[3][0] = "4"; // id
MyArray[3][1] = "ohm"; //name
MyArray[3][2] = "29"; //old
MyArray[3][3] = "16/09/57"; // register

to JSON:

  "person": {
    "MyAray": [
        "id": "1",
        "name": "bat",
        "old": "12",
        "register": "16/05/57"
        "id": "2",
        "name": "john",
        "old": "22",
        "register": "16/03/57"
        "id": "3",
        "name": "Sala",
        "old": "12",
        "register": "16/01/57"
        "id": "2",
        "name": "ohm",
        "old": "29",
         "register": "16/09/57"

and sent json with tcp_client
What should I do?

Try something like this, I think that should work.

    "id": MyArray[0][0],
    "name": MyArray[0][1],
    "old": MyArray[0][2],
    "register": MyArray[0][3]

You have to carve the json string by hand.

This how I do it:
//This is a snipped of WORKING code!
 String strState;

    if (millis() - update_cnt > 1000 || override == true) {
        update_cnt = millis();
        // do_algorithm(TWO_PUMP_AND_VENTILATOR);
        strState += "{'v':8,";
        strState += "'state':";
        strState += sv.state;
        strState += ",'st':";
        strState += sv.start_timer;
        strState += ",'mwt':"; //minimum water temp
        strState += sv.pump_start_temp;
        strState += ",'wps':";
        strState += sv.water_pump_stop_temp;
        strState += ",'h_state':";
        strState += sv.home_state;
        //strState += ",'al':";
        //strState += sv.alarm_led;
        strState += ",'wp':";
        strState += sv.water_pump;
        strState += ",'ap':";
        strState += sv.accu_pump;
        strState += ",'ve':";
        strState += sv.ventilator;
        strState += ",'tt':";
        strState += ftoa(buf, sv.target_temp, 1);
        strState += ",'it':",
        strState += ftoa(buf, sv.in_temp, 1);
        //strState += ",'ot':";
        //strState += ftoa(buf, sv.out_temp, 1);
        strState += ",'wt':";
        strState += ftoa(buf, sv.water_temp, 1);
        strState += ",'at':";
        strState += ftoa(buf, sv.accu_temp, 1);
        strState += ",'ct':";
        strState += ftoa(buf, sv.chimney_temp, 1);
        strState += ",'np':";
        strState += sv.ntc_pad;
        strState += ",'aw':";
        strState += ftoa(buf, sv.away_temp, 0);
        strState += ",'nf':";
        strState += ftoa(buf, sv.noFrost_temp, 0);
        strState += ",'mbt':";
        strState += ftoa(buf, sv.min_accu_temp, 0);
        strState += ",'act':";
        strState += ftoa(buf, sv.accu_charge_temp, 0);
        strState += ",'apt':";
        strState += sv.acku_pump_timer;
        strState += ",'ut':";
        strState += sv.up_time;
        strState += "}";
        strState.getBytes(system_state, sizeof (system_state) - 1, 0);
#ifdef VERBOSE


For an array I suggest you create a subroutine with parameter int index that points to the array member.

String json;

void createJSON(){

json += "{ "person": {"MyAray": [";  <<fiddle the exact format out ;)
for (int ii = 0 ; ii < numMembers;ii++){
json +=" ] }}"";  //<<fiddle the number of curlies and braces out ;)

void addMember(int index){
json +="  {";        //read closely, this code contains sequences of ' ". the single quotes are to 
json +="id": " ;    //surround items, the doubles are to embrace strings.
json += "ndx" ;   
json += ",'name':'";
json += array[index][0];
json+= "'old':"; 
json += array[index][1]
json += "},"  //I did test the correct syntax. Just compile and debug!

Good luck, if you want to collect your array with a REST call, make sure the json string is not bigger than 600 chars.

One word of caution when using that many String concatenations (or other String methodes).
Due to the implementation of the String class, each of these operations does allocate a new block of heap mem, so you’ll run the risk of fragmenting and eventually exhausting your heap space.

1 Like

O you mean that every += allocates a new chunk of memory, :unamused:
I run my application for over a year with no hangups. It calls that string function every 3 seconds.
So apperently the fragmentation does not kill the Core. Since yesterday I publish and measure with the dashboard, we’ll see how many nightly restarts we actually have :wink: String classes are, named to be dangerous in micro controller world, but I must say that the spark implementation is stable, unlike the Arduino’s.

I must admit, it’s a while since I had a look at the implementation of String :blush:

But it’s good to hear, that you can say out of experience, that suspected issues don’t bite as hard as I thought :wink:

Spark and Arduino String classes are essentially the same. The both suffer from fragmentation issues depending on the code that is used.

In the above example, most of the string manipulation is adding const and non-const char arrays to the end of string. In this case the string grows to fit the contents and never really shrinks. Not a lot of bad things can happen.

The code that gets you in trouble is substring and other methods of String that return an entire newly allocated String object every time they are called. That leads to problems, sometimes very quickly.

You have to understand the underlying memory model to get really understand what is happening when you use the String object methods.

1 Like

Thanks @bko for that background :+1:

Building on that I’d suspect if there was one other instance of String involved and both would be growing alternately, each attempt to grow could call for a relocation of the String causing fragmentation again.

So there is nothing against using String as long you exactly know what’s happening behind the scenes - or just avoid it, if in doubt :wink: