#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define json_max 20
enum json_style
{
json_int=0,
json_string=1
};
struct json
{
char* name_start;
char* name_end;
char *child_start;
char *child_end;
struct json *child;
int child_len;
};
struct json * str2json(char **start,int *len)
{
char *i = NULL,*j,*k1,*k2;
struct json *c;
*len = 0;
c = (struct json*)malloc(sizeof(struct json)*json_max);
memset(c,0,sizeof(struct json)*json_max);
c[*len].name_start = *start;
while((**start)!=0)
{
if(**start == '{')
{
*start = *start + 1;
c[*len].child = str2json(start,&c[*len].child_len);
continue;
}
else if(**start == 58)
{
c[*len].name_end = *start - 1;
c[*len].child_start = *start + 1;
}
else if(**start == '}')
{
c[*len].child_end = *start - 1;
*len = *len + 1;
*start = *start + 1;
return c;
}
else if(**start == ',')
{
c[*len].child_end = *start - 1;
*len = *len + 1;
c[*len].name_start = *start + 1;
}
*start = *start + 1;
}
return c;
}
void show(struct json *j,int l)
{
int i = 0;
char tmp[50];
for(i = 0; i < l ;i++)
{
memset(tmp,0,sizeof(tmp));
strncpy(tmp,j[i].name_start,j[i].name_end - j[i].name_start + 1);
printf("%s ",tmp);
if((&j[i])->child_len !=0)
{
show((&j[i])->child,(&j[i])->child_len);
}
else
{
memset(tmp,0,sizeof(tmp));
strncpy(tmp,j[i].child_start,j[i].child_end - j[i].child_start + 1);
printf("%s \n",tmp);
}
}
}
void getJsonValue(struct json *j,int l,char *input,char* output,enum json_style jstyle)
{
int i = 0;
char tmp[50];
for(i = 0; i < l &&strlen(output) == 0;i++)
{
memset(tmp,0,sizeof(tmp));
strncpy(tmp,j[i].name_start + 1,j[i].name_end - j[i].name_start - 1);
if(strcmp(tmp,input) == 0)
{
if((&j[i])->child_len !=0)
{
output = NULL;
}
else
{
memset(tmp,0,sizeof(tmp));
strncpy(tmp,j[i].child_start + jstyle,j[i].child_end - j[i].child_start + 1 - 2*jstyle);
strcpy(output,tmp);
}
return;
}
if((&j[i])->child_len !=0)
{
getJsonValue((&j[i])->child,(&j[i])->child_len,input,output,jstyle);
}
}
}
void delJson(struct json *j,int l)
{
int i = 0;
for(i = 0; i < l ;i++)
{
if((&j[i])->child_len !=0)
{
delJson((&j[i])->child,(&j[i])->child_len);
}
}
free(j);
}
int f(void)
{
//char S[] = "{\"a\":1,\"b\":2}";
//char S[] = "{\"a\":{\"b\":2}}";
char S[] = "{\"a\":{\"b\":{\"c\":3}}}";
//char S[] = "{\"a\":[{\"b\":2},{\"c\":3}]}";//无法解析
char *s;
struct json *j;
char json_str[2000] = {0};
int l;
char tmp[50];
char *json_i,*json_j,*json_k;
//printf("%s",S);
strcat(json_str,"root:");
json_j = strchr(S,'{');
json_k = strrchr(S,'}');
json_i = json_str + strlen("root:");
strncpy(json_str + strlen("root:"),json_j,json_k-json_j + 1);
//printf("%s\n",json_str);
s = json_str;
j = str2json(&s,&l);
show(j,1);
memset(tmp,0,sizeof(tmp));
getJsonValue(j,1,"b",tmp,json_int);
printf("%s\n",tmp);
memset(tmp,0,sizeof(tmp));
getJsonValue(j,1,"c",tmp,json_int);
printf("%s\n",tmp);
delJson(j,1);
}
int main()
{
f();
}
主要的是实现json的字符串转为自定义的json结构体
需要注意的是下面这一段
strcat(json_str,"root:");
在json的字符串前面添加“root:”,就和构造队列时用到的那个无用的头一个意思。
解析过程用的是递归。
解析过程并不完善,例如下面这一段json的字符串就无法解析,之后再完善吧。
char S[] = "{\"a\":[{\"b\":2},{\"c\":3}]}";