새로운 토큰을 파싱한다
해당 메뉴를 store.c에 파싱한다. Makefile에서 실행시킨다
{
"Yori":
[
{
"name":"Haemultang",
"size":"Jong",
"price":50000
},
{
"name":"Haemultang",
"size":"Dae",
"price":60000
},
{
"name":"Haemuljimg",
"size":"Jong",
"price":50000
},
{
"name":"Haemuljim",
"size":"Dae",
"price":60000
}
],
"Sicksa":
[
{
"name":"BJnoodle",
"size":"1 piece",
"price":7000
},
{
"name":"FIredSquid",
"size":"1 piece",
"price":8000
},
{
"name":"Rice with Suisi",
"size":"1 piece",
"price":10000
}
]
}
store.c 및 여러 fucntion
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jsmn.h"
/*
* A small example of jsmn parsing when JSON structure is known and number of
* tokens is predictable.
*/
//void printkey(const char * a, jsmntok_t *b,int c );
static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
return 0;
}
return -1;
}
char * jsmn_StringReader(const char * fileName){
char a[20];
char * b;
FILE *f1;
f1=fopen(fileName,"r");
b=(char *)malloc(sizeof(char));
//a=(char*)malloc(sizeof(char));
while(1){
fgets(a,sizeof(a),f1);
if(feof(f1))break;
b=(char*)realloc(b,strlen(b)+strlen(a));
//printf("\n%d\n",strlen(a));
strncat(b,a,strlen(a));
}
fclose(f1);
return b ;
}
void printall(const char *json,jsmntok_t *t,int tok_count ){
int i=0;
char t_type[20];
for( i=1;i<tok_count;i++){
printf("[%d] %.*s (size: %d, %d~%d,",i,t[i].end-t[i].start, json + t[i].start,t[i].size,t[i].start,t[i].end);
switch(t[i].type){
case 0: strcpy(t_type,"JSMN_UNDEFIEND");break;
case 1: strcpy(t_type,"JSMN_OBJECT");break;
case 2: strcpy(t_type,"JSMN_ARRAY");break;
case 3: strcpy(t_type,"JSMN_STRING");break;
case 4: strcpy(t_type,"JSMN_PRIMITIVE");break;
default: break;
}
printf("%s) \n",t_type);
}
}
typedef enum{
C_SET=0,
C_MEAL=1
} mycategory_t;
typedef struct{
mycategory_t cat;
char name[20];
char size[10];
int price;
} mymenu_t;
void printmenu(mymenu_t*m[], int count){
int i=0;
for(i=0;i<count;i++){
switch(m[i]->cat){
case 0: printf("Yori-->\n");break;
case 1: printf("Sicksa-->\n");break;
}
printf("이름: %s\n",m[i]->name);
printf("크기: %s\n",m[i]->size);
printf("가격: %d\n",m[i]->price);
printf("\n");
}
}
int makemymenu(const char *json, jsmntok_t *t, int r, mymenu_t* m[]){
int size=0;
int i=0,j=0,menucount=0;
int type=0;
char through[20];
for(i=1;i<r;i++){
if(t[i+1].type==2){
if( !strncmp("Yori", json + t[i].start,t[i].end-t[i].start)){type=0;}
else if ( !strncmp("Sicksa", json + t[i].start,t[i].end-t[i].start)){type=1;}
else type =0;
size=t[i+1].size;
i+=4;
for(j=menucount;j<menucount+size;i+=3,j++){
m[j]->cat=type;
strncpy(m[j]->name, json + t[i].start,t[i].end-t[i].start);i+=2;
strncpy(m[j]->size,json + t[i].start,t[i].end-t[i].start);i+=2;
strncpy(through,json+t[i].start,t[i].end-t[i].start);
m[j]->price=atoi(through);
}i-=3;
menucount+=size;
}
}
return menucount;
}
int order(mymenu_t *m[],int count){
int answer;
int receits=0;
int i=0;
while(1){
//aks
printf("Would you like to order? (1.yes 2.no)\n");
scanf("%d",&answer);
if(answer==2)break;
while(1){
for(i=0;i<count;i++){
printf("[%d]\tname: %s\n\t%s\n\t%d won\n\n",i+1,m[i]->name,m[i]->size,m[i]->price);
}
printf("Chose food you want to eat\n");
scanf("%d",&answer);
if(answer<1||answer>count)
printf("Type right index please.......\n");
else break;
}
receits+=m[answer-1]->price;
}
printf("You have orderd %d won \n",receits);
return receits;
}
void jsmn_init(jsmn_parser *parser);
int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,jsmntok_t *tokens, unsigned int num_tokens);
int main() {
int keyarray[128],keyamount;
int i;
int r;
int menucount;
jsmn_parser p;
jsmntok_t t[128]; /* We expect no more than 128 tokens */
mymenu_t * mymenu[20];
for(i=0;i<20;i++)
mymenu[i]=(mymenu_t*)malloc(sizeof(mymenu_t));
char fileName[20];
jsmn_init(&p);
printf("Insert a file Name:");
scanf("%s",fileName);
char * JSON_STRING=jsmn_StringReader(fileName);
r = jsmn_parse(&p, JSON_STRING, strlen(JSON_STRING), t, sizeof(t)/sizeof(t[0]));
<<<<<<< HEAD
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]);
printvalues(JSON_STRING,t,r,keyarray);
return EXIT_SUCCESS;
=======
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]);
int k;
menucount=makemymenu(JSON_STRING,t,r,mymenu);
printf("\n\n\n menucount: %d\n",menucount);
printmenu(mymenu,menucount);
order(mymenu,menucount);
return EXIT_SUCCESS;
>>>>>>> 94ae9aa240ab50babaf259b9275e72cc0fd9856d
}
------------------------------------------------------------------------------------------------------------------------------------------
Output)
'OSS' 카테고리의 다른 글
[MAC&android] localhost를 기반으로 LoginRegister UI 만들기 (0) | 2018.07.17 |
---|---|
[jsmn]주문서를 받아서 매출 계산하기 (0) | 2018.06.08 |
[jsmn]메뉴정보를 출력하기 (0) | 2018.06.08 |
[jsmn]파싱된 토큰으로 새로운 구조체에 저장하기 (1) | 2018.06.08 |
[jsmn]token 번호를 통해서 value를 출력하는 printvalue() (0) | 2018.05.28 |