C 语言实现 JSON 解析器
#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) {/解析数值/
}
const char* parse_string(CJSON *node, const char *buf) {/解析字符串/
}
const char* parse_object(CJSON *node, const char *buf) {/解析对象/
} // const char* parse_array(CJSON *node, const char *buf) {/解析数组/
} 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;
}
原文地址: https://www.cveoy.top/t/topic/otPU 著作权归作者所有。请勿转载和采集!