Page 1 of 1

search text in a array ( inbdexof )

Posted: Fri Jul 07, 2023 10:02 am
by dje8269
Hello,

I'm a neewby in ESP-IDF .

After many attempts I can't find a text in a table with the indexOf function?

here is my function which logs the response from the server :
  1. /***************************   HTTP  GET    ************************************/
  2. esp_err_t client_event_get_handler(esp_http_client_event_handle_t evt)
  3. {
  4.     char buf_data[1000];
  5.  
  6.     switch (evt->event_id)
  7.     {
  8.         case HTTP_EVENT_ON_DATA:
  9.             sprintf(buf_data, "%.*s\n", evt->data_len, (char *)evt->data);   //  printf("%.*s\n", str_len, str);
  10.             printf(buf_data);
  11.         break;
  12.  
  13.     default:
  14.         break;
  15.     }
  16.     return ESP_OK;
  17. }
with a GET command I receive a response from my server that looks like this:

{"LED1":"1","LED2":"0","LED3":"1","INTER":"1","POTAR1":"1993","POTAR2":"1401"," BUZ":"0","MAJ":"56","MAJ_web":"56","MAJ_ESP":"56"}

I would like to be able to search for the text for example "LED1":"1" to know if I should turn my led on or off.

I have try this :
  1. /***************************   HTTP  GET    ************************************/
  2. esp_err_t client_event_get_handler(esp_http_client_event_handle_t evt)
  3. {
  4.     char buf_data[1000];
  5.  
  6.     switch (evt->event_id)
  7.     {
  8.         case HTTP_EVENT_ON_DATA:
  9.             sprintf(buf_data, "%.*s\n", evt->data_len, (char *)evt->data);   //  printf("%.*s\n", str_len, str);
  10.             printf(buf_data);
  11.         break;
  12.  
  13.     default:
  14.         break;
  15.     }
  16.  
  17.     if ( buf_data.IndexOf("LED1\":\"1") > 0)
  18.     {
  19.         gpio_set_level(LED_BLEUE, 1);
  20.     }
  21.     else
  22.     {
  23.         gpio_set_level(LED_BLEUE, 0);
  24.     }
  25.  
  26.     return ESP_OK;
  27. }
But I have an error :
error: request for member 'IndexOf' in something not a structure or union
143 | if ( buf_data.IndexOf("LED1\":\"1") > 0)
| ^
ninja: build stopped: subcommand failed.
Can you help me to find a solution ?
Thanks for reading me

Jérémy

Re: search text in a array ( inbdexof )

Posted: Sat Jul 08, 2023 3:27 am
by ESP_Sprite
You're not programming in C++ / Arduino, buf_data is not a string. You can use strstr() to get a pointer to whatever you need, but it looks like your input is JSON, so I'd really advise using e.g. the cJSON library to squirrel out the correct data rather than using hacky and fragile string operations.