Search code examples
phpjsonesp32

Create a JSON array with php


So, I'm trying to create an array that looks like this, so I can access the stuff thats in it easier. I am using an ESP32 project, and it uses Json 6 library, for some reason I can't access the stuff that is in the array that I have created, what I mean is that this int id1 = doc[1]["AutoIncrement"]; is not working, I can only gather the first part of the array.

{"AutoIncrement" : [0,1,2], "Aparelho": [LED, lED1, LED2], "Status": [0,1,0]}

I could accomplish to make it like this, But in this array I have problem acessing the stuff thats in it, if I have more than 2 lines of content.

[{"AutoIncrement":"1","Aparelho":"LED","Status":"0"},{"AutoIncrement":"2","Aparelho":"LED1","Status":"1"},{"AutoIncrement":"3","Aparelho":"LED2","Status":"0"}]

This is the php code:

sql = "SELECT * FROM dados";
   $result = mysqli_query($conn, $sql);
   $json_array = array();
   while($row = mysqli_fetch_assoc($result))
   {
       $json_array[] = array(
           'AutoIncrement'         =>       $row["AutoIncrement"],
           'Aparelho'         =>       $row["aparelho"],
           'Status'         =>       $row["situacao"],
       );
   }
   return json_encode($json_array);
}
$file_name = 'dadosjson' . '.json';

ESP32 part if you want to have a loook:

 String payload = http.getString();
    Serial.println("\nStatuscode: "+ String(httpCode));
    Serial.println(payload);

    char json[500];
    payload.replace(" ", "");
    payload.replace("\n", "");
    payload.trim();
    payload.remove(0,1);
    payload.toCharArray(json, 500);

    StaticJsonDocument<200> doc;
    deserializeJson(doc, json);

    //Isso nao depende do nome no banco de dados, depende do nome dentro de action.php          
    const char* nome = doc[0]["Aparelho"];
    int stat = doc[0]["Status"];

    int id1 = doc[1]["AutoIncrement"];
    const char* nome1 = doc[1]["Aparelho"];
    int stat1 = doc[1]["Status"];

    int id2 = doc["AutoIncrement"][2];
    const char* nome2 = doc["Aparelho"][2];
    int stat2 = doc["Status"][2];

    Serial.println(id2);

Result of print id2, is 0, but was suppose to be 2


Solution

  • The following code do more harm than help for the deserialisation of the JSON object and really unnecessary that you should remove it.

    payload.replace(" ", "");
    payload.replace("\n", "");
    payload.trim();
    payload.remove(0,1);
    

    If your payload looks like this:

    "[{\"AutoIncrement\":\"1\",\"Aparelho\":\"LED\",\"Status\":\"0\"}, {\"AutoIncrement\":\"2\",\"Aparelho\":\"LED1\",\"Status\":\"1\"}, \"AutoIncrement\":\"3\",\"Aparelho\":\"LED2\",\"Status\":\"0\"}]";
    

    All you need is directly convert the String object into a c char array and then deserialised it. You also have mistakes in accessing the deserialised object such as doc["AutoIncrement"][2] when it should be doc[2]["AutoIncrement"].

    String payload = http.getString();
    Serial.println("\nStatuscode: "+ String(httpCode));
    Serial.println(payload);
    
    // converting String payload into a c char array
    int length = payload.length() + 1;
    char json[length];
    payload.toCharArray(json, length);
    
    StaticJsonDocument<200> doc;
    deserializeJson(doc, json);
    
    //LED
    int id = doc[0]["AutoIncrement"];
    const char* nome = doc[0]["Aparelho"];
    int stat = doc[0]["Status"];
    
    //LED 1
    int id1 = doc[1]["AutoIncrement"];
    const char* nome1 = doc[1]["Aparelho"];
    int stat1 = doc[1]["Status"];
    
     //LED 2
    int id1 = doc[2]["AutoIncrement"];
    const char* nome2 = doc[2]["Aparelho"];
    int stat1 = doc[2]["Status"];
    
    Serial.println(id);
    Serial.println(nome);
    Serial.println(stat);
    
    Serial.println(id1);
    Serial.println(nome1);
    Serial.println(stat1);
    
    Serial.println(id2);
    Serial.println(nome2);
    Serial.println(stat2);
    

    This will produce the correct results as:

    1
    LED
    0
    2
    LED1
    1
    3
    LED2
    0