Json-c编写一个简单的 Json parser

这篇博客介绍了如何利用Json-c库构建一个简单的Json解析器。通过编译和运行示例代码,展示了如何实现Json的缩进效果,提供了一个完整的代码示例,并给出了参考链接以供深入学习。

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

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);
}

运行效果:
这里写图片描述

参考链接

A simple and complete json parser

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值