0

I'm trying to parser a JSON data into two dimentional char array by using strtok(). but it doesn't work well. ( I know strtok will change the original file, so I have a temperary buffer but it doesn't work)

Here is the JSON data:

{"data":[
[13,0,31,20,17,59],
[5,1,0,0,0,0],
[0,9,26,24,0,1],
[7,9,57,31,0,0],
[5,1,0,0,0,0]
]}

Here is my code:

int parser(char *buffer){

      char *pStr;
      char *token;
      char *t;
      char *p;
      int size;
      char temp[100];
      pStr = strstr(buffer, "{\"data\":[[");
      //offet by 9 to reach the JSON data
      pStr=&pStr[9];
      token=strtok(pStr,"[");
      printf(token);

      while(token != NULL)
      {
          p = strstr(token, "]");
          size=p-token;
          memcpy(temp, token, size);
          t=strtok(temp,",");
          while(t != NULL){
                      printf(t);
              t = strtok(NULL, ",");
          }
          token = strtok(NULL, "[");
      }
    return 0;
}

the result looks the temporary buffer doesn't work. Did I miss something? Thanks a lot.

13,0,31,20,17,59],
13
0
31
20
17
59
Vincent Zhou
  • 503
  • 1
  • 6
  • 17
  • Why would you implement your own json parser? If this is not for learning I would __strongly__ suggest against it, there are plenty of json parsers out there that produce valid results and are tested rapidly. – Benjamin Gruenbaum Jan 31 '13 at 20:31
  • Please 1. Don't try to parse JSON using your own code ([unless you're me](http://github.com/H2CO3/libjsonz)), 2. Don't write "dimentional" because it hurts my eyes. Thanks! –  Jan 31 '13 at 20:35
  • Thanks. I got a good answer from :http://stackoverflow.com/questions/4693884/nested-strtok-function-problem-in-c/4694282#4694282 – Vincent Zhou Feb 21 '13 at 17:27

0 Answers0