Json-c编写一个简单的 Json parser
分享一个使用 Json-c 编写的简单的 Json Parser:
#include <json-c/json.h>
#include <stdio.h>
void print_json_value(const char* key, json_object *jobj) {
enum json_type type = json_object_get_type(jobj);
switch(type) {
case json_type_null:
printf("Type: json_type_null, value: null\n");
break;
case json_type_boolean:
printf("Type: json_type_boolean, key: %s, value: %s\n", key, (json_object_get_boolean(jobj) ? "true" : "false"));
break;
case json_type_double:
printf("Type: json_type_double, key: %s, value: %lf\n", key, json_object_get_double(jobj));
break;
case json_type_int:
printf("Type: json_type_int, key: %s, value: %d\n", key, json_object_get_int(jobj));
break;
case json_type_object:
printf("Type: json_type_object, key: %s\n", key);
break;
case json_type_array:
printf("Type: json_type_array, key: %s\n", key);
break;
case json_type_string:
printf("Type: json_type_string, key: %s, value: %s\n", key, json_object_get_string(jobj));
break;
}
}
void json_parse_array(json_object *jobj, char* key) {
void json_parse(json_object *jobj);
enum json_type type;
json_object *jarray = jobj;
if(key) {
jarray = json_object_object_get(jobj, key);
}
int arrLen = json_object_array_length(jarray);
printf("\tKey: %s, Array Length: %d\n", key, arrLen);
int i;
json_object *jvalue;
for(i=0; i<arrLen; i++) {
jvalue = json_object_array_get_idx(jarray, i);
type = json_object_get_type(jvalue);
if(type == json_type_array) {
printf("******************************\n");
json_parse_array(jvalue, NULL);
printf("******************************\n");
} else if(type != json_type_object) {
printf("\tvalue[%d]: ", i);
print_json_value(key, jvalue);
} else {
printf("******************************\n");
json_parse(jvalue);
printf("******************************\n");
}
}
}
void json_parse(json_object *jobj) {
enum json_type type;
json_object_object_foreach(jobj, key, jval) {
type = json_object_get_type(jval);
switch(type) {
case json_type_null:
case json_type_boolean:
case json_type_double:
case json_type_int:
case json_type_string:
print_json_value(key, jval);
break;
case json_type_object:
print_json_value(key, jval);
printf("******************************\n");
json_parse(jval);
printf("******************************\n");
break;
case json_type_array:
print_json_value(key, jval);
json_parse_array(jobj, key);
break;
}
}
}
int main(int argc, char* argv[])
{
char * string = "{ \
\"sitename\" : \"joys of programming\", \
\"categories\" : [ \"c\" , [\"c++\" , \"c\" ], \"java\", \"PHP\" ], \
\"author-details\": \
{ \
\"admin\": false, \
\"name\" : \"Joys of Programming\", \
\"Number of Posts\" : 10, \
\"Last Log In\" : \"2017-05-22 11:18\" \
} \
}";
printf("JSON string: \n%s\n", string);
printf("\n############################\n");
printf("JSON Parser: \n");
json_object * jobj = json_tokener_parse(string);
json_parse(jobj);
return 0;
}
编译运行:
gcc -o jparser demo2.c -ljson
运行效果:
增加缩进效果
代码:
#include <json-c/json.h>
#include <stdio.h>
#include <string.h>
static char* g_jsonType[] = {"json_type_null", "json_type_boolean", "json_type_double",
"json_type_int", "json_type_object", "json_type_array",
"json_type_string"};
const char* _jsonType(enum json_type type) {return g_jsonType[type];}
void dispJsonValue(const char* indent, const char* key, json_object *object, enum json_type type)
{
switch(type) {
case json_type_null:
printf("%skey: %s, type: %s, value: null\n", indent, key, _jsonType(type));
break;
case json_type_boolean:
printf("%skey: %s, type: %s, value: %s\n", indent, key, _jsonType(type), (json_object_get_boolean(object) ? "true" : "false"));
break;
case json_type_double:
printf("%skey: %s, type: %s, value: %lf\n", indent, key, _jsonType(type), json_object_get_double(object));
break;
case json_type_int:
printf("%skey: %s, type: %s, value: %d\n", indent, key, _jsonType(type), json_object_get_int(object));
break;
case json_type_string:
printf("%skey: %s, type: %s, value: %s\n", indent, key, _jsonType(type), json_object_get_string(object));
break;
default:
break;
}
}
void parse(const char* indent, const char* key, json_object *object)
{
enum json_type type = json_object_get_type(object);
char* newIndent = malloc(strlen(indent)+2);
sprintf(newIndent, "%s\t", indent);
if(type == json_type_object) {
printf("%skey: %s, type: %s. size: %d\n", indent, key, _jsonType(type), json_object_object_length(object));
printf("%s{\n", indent);
json_object_object_foreach(object, key, value) {
parse(newIndent, key, value);
}
printf("%s}\n", indent);
} else if(type == json_type_array) {
int arrayLength = json_object_array_length(object);
printf("%skey: %s, type: %s. size: %d\n", indent, key, _jsonType(type), arrayLength);
printf("%s[\n", indent);
int i;
for(i=0; i<arrayLength; i++) {
json_object* value = json_object_array_get_idx(object, i);
char* newKey = malloc(128);
sprintf(newKey, "Node %d", i+1);
parse(newIndent, newKey, value);
free(newKey);
}
printf("%s]\n", indent);
} else {
dispJsonValue(indent, key, object, type);
}
free(newIndent);
}
运行效果: