<findkeys()>



#include <stdio.h>

#include <string.h>

#include "jsmn.h"






int findkeys(const char *json, jsmntok_t *t, int tokcount, int * keys){ int i,j=0;

for(i=1;i<tokcount;i++){

if(t[i].size>=1&&t[i].type==3){

keys[j++]=i;

}

}

return j;


}


설며이: 코드는 간단하다. parsing된 token들의 데이터를 토대로 조건문과 반복문을 돌려 size가 1이 이상인경우 그리고 JSMN_STRING인 경우의 token만 keys (parse.c에서는 int array의 argument를 받는다)에 저장하게 된다.




keyamount=findkeys(JSON_STRING,t,r,keyarray);

printall(JSON_STRING,t,r);

printkeys(JSON_STRING,t,r);

printf("Keyamount is %d\n",keyamount);

int b=0;for( b=0;b<keyamount;b++)printf("--> %d\n",keyarray[b]);

return EXIT_SUCCESS;

}


parsejson.c  파일 하단에 findkeys함수와 그 값을 출력하는 값을 출력 하였다. 

<Output>

그럼 다음과 같은 결과 값이 나온다. keyarray에 저장된 array 수는 총 10개이며 각 index에는 parsing된 token의 token번호가 순서대로 저장되어있다.

+ Recent posts