用c实现一个json解析【并不完善】

本文介绍了一种将JSON字符串转换为自定义结构体的方法,通过递归解析实现复杂JSON数据的处理。文章详细展示了如何使用C语言定义结构体来存储JSON数据,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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}]}";

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值