Advertisement
AntonioVillanueva

Search Token returns value

Dec 2nd, 2022
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int findToken (char* response,char* cherche){
  6.     const char * separators = ",";//Separateurs
  7.     char * strToken = strtok ( response, separators );//Tokenize
  8.    
  9.     while ( strToken != NULL ) {       
  10.         if ( strstr(strToken, "value") !=0){//Value in strToken ?
  11.             const char ch = ':';
  12.             return atoi ( strchr(strToken, ch)+1);//Search : +1
  13.         }
  14.         strToken = strtok ( NULL, separators );//Next Token
  15.     }
  16.    
  17.     return -1;
  18. }
  19.  
  20. int main (){
  21.    
  22.    
  23.     char response[]=" {\"status\": \"success\", \"data\": {\"counter_modes\": [\"Enabled\", \"Disabled\"], \"glob_dev_id\": 1, \"modes\": [\"Simple\", \"DirectSwitch\"], \"value\": 1, \"circuit\": \"2_01\", \"debounce\": 50, \"counter\": 2, \"counter_mode\": \"Enabled\", \"dev\": \"input\", \"mode\": \"Simple\"}} ";
  24.    
  25.  
  26.     printf ("%d\n",findToken (response,"value"));
  27.    
  28.    
  29.     return 0;
  30. }
  31.  
Tags: token
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement