Dealing with JSON for sending values

Hi,
For a week I have been searching for a way to deal with the values sent by JSON in a separate manner, but I didn't find the solution: Im using code in this link:
https://randomnerdtutorials.com/esp32-client-server-wi-fi/
and I change it to be suitable with what i need.

for (int i=0;  i < sizeof(a); i++) {
        json += "," + (String)a[i];
    }   
       server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(200, "application/json",json);
  });

and this is client

if(WiFi.status()== WL_CONNECTED ){ 
      temperature = httpGETRequest(serverNameTemp);
      char str_array[temperature.length()];
      temperature.toCharArray(str_array, temperature.length());
      char* token = strtok(str_array, " ");
      int value = token.toInt();
      value+=3;

Best,

Perhaps I missed it, but what is the question exactly?

I received the array in like this manner

[22,23,27,18,19,20,21,25,23,22]

as a string and I need to deal with each value as a separate value. I used this code but is not helpful , how can I received it as int array and add number for each value?

if(WiFi.status()== WL_CONNECTED ){ 
      temperature = httpGETRequest(serverNameTemp);
      char str_array[temperature.length()];
      temperature.toCharArray(str_array, temperature.length());
      char* token = strtok(str_array, " ");
      int value = token.toInt();
      value+=3

;

It's not helpful because it doesn't even compile. Might have been worth mentioning.

Are we talking about an ESP controller? In that case (not in the case of an AVR Arduino) use the String class. It has the indexOf() method that helps tokenizing (together with substring()) and it offers the toInt() method.

Yes, esp, any code to do that please?

Just an example:

uint16_t pos = myString.indexOf('[');
uint16_t start = pos + 1;
while ((pos = myString.indexOf(',', start)) >= 0) {
  int16_t part = myString.substring(start, pos).toInt();
  start = pos + 1;
}
int16_t lastpart = myString.substring(start, myString.indexOf(']', start));

This gives you the content of the array in part and lastpart.
You have to adapt it to your needs.

How can use string with int? And is lastpart represent the last number in array?

Or is there any another way to sent this array by wifi? ? :disappointed_relieved:

I don't know what you mean.

Yes.

There are dozens. Many probably not ideal for your task, even binary is possible if you insist only on WiFi but not on HTTP, although even HTTP offers the binary option.

I just wanted to send and receive int values ​​using WiFi :

A fixed number of them? What value range? Provide much more details about the usage you have.

No, not fixed some time 20,100,1000.. l am just I do not know how to send array with wifi and receive it to deal with this values.
Thanks,

I already sent you some example code. It works for a correctly formatted JSON array but doesn't detect errors. As you don't provide details, this code should work for you.

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