完整代码如下:

#include <bits/stdc++.h> #define cJSON_False 0 #define cJSON_True 1 #define cJSON_NULL 2 #define cJSON_Number 3 #define cJSON_String 4 #define cJSON_Array 5 #define cJSON_Object 6

int dep = 0; typedef struct cjson { int type; char *kstring; char *vString; int vInt; double vDouble; struct cjson *next, *prev; struct cjson *child; }CJSON;

const char *parse_buf(CJSON *node, const char *str); //给节点分配堆内存 CJSON *JSON_New_Node() { CJSON * node = (CJSON *)malloc(sizeof(CJSON)); if (node) { memset(node, 0, sizeof(CJSON)); } return node; } //过滤换行 空格 回车 等等不可见字符 const char *filter(const char *p) { while (p && *p && ((unsigned char)*p<=32)) { p++; } return p; }

const char* parse_number(CJSON *node, const char *str) {/解析数值/ char *end; node->type = cJSON_Number; node->vDouble = strtod(str, &end); return end; }

const char* parse_string(CJSON *node, const char *buf) {/解析字符串/ char *end; node->type = cJSON_String; buf++; end = strchr(buf, '"'); node->vString = (char *)malloc((end - buf + 1) * sizeof(char)); strncpy(node->vString, buf, end - buf); node->vString[end - buf] = '\0'; return end + 1; }

const char* parse_object(CJSON *node, const char *buf) {/解析对象/ node->type = cJSON_Object; buf++; node->child = JSON_New_Node(); node->child->next = NULL; node->child->prev = NULL; node->child->kstring = NULL; node->child->vString = NULL; node->child->child = NULL;

while (*buf != '}')
{
	buf = filter(buf);
	//解析 key
	buf = parse_string(node->child, buf);
	node->child->kstring = node->child->vString;
	node->child->vString = NULL;
	buf = filter(buf);
	//解析 :
	buf++;
	//解析 value
	buf = parse_buf(node->child, buf);
	//下一个节点
	node->child->next = JSON_New_Node();
	node->child->next->prev = node->child;
	node->child->next->kstring = NULL;
	node->child->next->vString = NULL;
	node->child->next->child = NULL;
	node->child = node->child->next;
}
return buf + 1;

} // const char* parse_array(CJSON *node, const char *buf) {/解析数组/ node->type = cJSON_Array; buf++; node->child = JSON_New_Node(); node->child->next = NULL; node->child->prev = NULL; node->child->kstring = NULL; node->child->vString = NULL; node->child->child = NULL;

while (*buf != ']')
{
	buf = filter(buf);
	//解析 value
	buf = parse_buf(node->child, buf);
	//下一个节点
	node->child->next = JSON_New_Node();
	node->child->next->prev = node->child;
	node->child->next->kstring = NULL;
	node->child->next->vString = NULL;
	node->child->next->child = NULL;
	node->child = node->child->next;
	buf = filter(buf);
	//解析 ,
	buf++;
}
return buf + 1;

} const char *parse_buf(CJSON *node, const char *str) { if (!str) return NULL; //如果是节点 if (*str == '{') { return parse_object(node, str); } //如果是字符串 if (*str == '"') { return parse_string(node, str); } //如果是数字 if (*str == '-' || (*str>='0' && *str <= '9')) { return parse_number(node, str); } //如果是数组 if (*str == '[') { return parse_array(node, str); } //如果是除上述之外的其他值 if (!strncmp(str,"false",5))//是false { node->type = cJSON_False; node->vInt = 0; return str + 5; } if (!strncmp(str,"true",4))//是true { node->type = cJSON_True; node->vInt = 1; return str + 4; } if (!strncmp(str,"null",4)) //是null { node->type = cJSON_NULL; return str + 4; }

return NULL;

}

int print_dep() { int i=0; while(i<dep) { printf("-> "); i++; } } //递归释放 并打印 写在一起了 也可以分开 int JSON_Delete(CJSON *c) { CJSON *next; while (c) { print_dep(); if (c->kstring) { printf("K:%s ",c->kstring); free(c->kstring); } if (c->type == cJSON_Number)printf("V:%lf\n",c->vDouble); else if (c->type == cJSON_String)printf("V:%s\n",c->vString); else if (c->type == cJSON_True)printf("V:true\n"); else if (c->type == cJSON_False)printf("V:false\n"); else if (c->type == cJSON_NULL)printf("V:null\n"); else if (c->type == cJSON_Object)printf("\n"); if (c->type == cJSON_Array)printf("ttt\n");

	if (c->vString)
	{	
		free(c->vString);
	}
	if (c->child)
	{
		dep++;
		JSON_Delete(c->child);
		dep--;
	}
	next = c->next;
	free(c);
	c = next;
}
return 0; 

} CJSON *JSON_Parse(const char *buf) { const char *end = NULL; CJSON *root = JSON_New_Node(); if (!root) return NULL;

end = parse_buf(root, filter(buf));
if (!end)
{
	printf("end error\n");
	JSON_Delete(root);
	return NULL;
}
return root;

} int main() { char buff[1024] = "{"name":"Arthur","attribute":{"attack":1920,"skill":{"one":1.88,"two":2.58,"three":3},"blood":5888},"type":"Warrior"}"; //char buff[1024] = "{"name":"Arthur","attribute":{"attack":1920,"skill":[1,2,3],"blood":5888},"type":"Warrior"}";

printf("json:%s\n\n", buff);
printf("Parse Result:\n\n");
CJSON *root_node = JSON_Parse(buff);
JSON_Delete(root_node);
return 0;
C语言实现JSON解析器

原文地址: https://www.cveoy.top/t/topic/otPQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录