#include #include #include #define TRUE 0 #define FALSE -1 int readJSON(char* buff, char* key, char* result); void main(void){ char json[129], result[1025]; printf("** TESTJSON : JSON の読取り **\n"); getchar(); strcpy(json, "{\ \"name\": \"PAUL\",\ \"description\": \"\"\ \"configuration_id\": \"{configuration_id}\",\ \"language\": \"en\"\ }"); printf("[%d] json = \n[%s]\n", __LINE__, json); readJSON(json, "name", result); printf("[%d] name = [%s]\n", __LINE__, result); getchar(); readJSON(json, "language", result); printf("[%d] language = [%s]\n", __LINE__, result); getchar(); } /************************************************/ int readJSON(char* json, char* key, char* result) /************************************************/ { char ckey[128], *ptr, buff[1025]; int pos; sprintf(ckey, "\"%s\"", key); if((ptr = strstr(json, ckey)) != NULL){/*FOUND*/ pos = (int)(ptr - json) + strlen(ckey) +1; while(json[pos] != '"') pos++; while(json[pos] != '"') pos ++; strcpy(buff, &json[pos+1]); buff[1024] = 0x00; if((ptr = strchr(buff, '"')) != NULL){/* 終わり */ pos = (int)(ptr - buff); buff[pos] = 0x00; }/* 終わり */ strcpy(result, buff); return TRUE; }/*FOUND*/ else return FALSE; }