前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >cJSON使用(二)

cJSON使用(二)

作者头像
李小白是一只喵
发布2020-04-24 08:37:20
9290
发布2020-04-24 08:37:20
举报
文章被收录于专栏:算法微时光

目录

cJSON使用(一)

cJSON使用(二)

实战举例

上文学习了cJSON库的一些理论知识,接下来开始进行实战.

从一个json文件中读取并解析这个文件.

代码实现

资源文件内容res.json:

代码语言:javascript
复制
{
  "config_appl": "file",
  "version": "0.1.0",
  "resources": {
      "key_p": "NA",
      "key_n": "WO",
      "key_e": "local",
      "key_ze": "na",
      "key_le": "na",
      "key_name": "na",
      "key_type": "file",
      "key_ID": "na"
  }
}

test.c代码:

代码语言:javascript
复制
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"cJSON.h"

char* read_file(const char *filename) {
    FILE *file = NULL;
    long length = 0;
    char *content = NULL;
    size_t read_chars = 0;

    /* open in read binary mode */
    file = fopen(filename, "rb");
    if (file == NULL)
    {
        goto cleanup;
    }

    /* get the length */
    if (fseek(file, 0, SEEK_END) != 0)
    {
        goto cleanup;
    }
    length = ftell(file);
    if (length < 0)
    {
        goto cleanup;
    }
    if (fseek(file, 0, SEEK_SET) != 0)
    {
        goto cleanup;
    }

    /* allocate content buffer */
    content = (char*)malloc((size_t)length + sizeof(""));
    if (content == NULL)
    {
        goto cleanup;
    }

    /* read the file into memory */
    read_chars = fread(content, sizeof(char), (size_t)length, file);
    if ((long)read_chars != length)
    {
        free(content);
        content = NULL;
        goto cleanup;
    }
    content[read_chars] = '\0';


cleanup:
    if (file != NULL)
    {
        fclose(file);
    }

    return content;
}

int main()
{
    char *json_file;
    cJSON * json_tmp;
    cJSON * ch, *key_ch;
    int size;
    int kye_size;
    int i, j;
    //使用官网函数读取文件(test/common.c)
    json_file = read_file("res.json");
    //解析json
    json_tmp = cJSON_Parse(json_file);

    //获取当前key值数量
    size = cJSON_GetArraySize(json_tmp);
    printf("%d\n", size);

    //判断数据类型
    ch = json_tmp->child;
    for(i = 0; i < size; i++){
        printf("[num=%d] [type=%d]\n", i, ch->type);
        ch = ch->next;
    }


    //打印每一个key值
    ch = json_tmp->child;
    for(i = 0; i < size; i++){
        if(cJSON_IsObject(ch)){
            kye_size = cJSON_GetArraySize(ch);
            key_ch = ch->child;
            printf("[num=%d          ]\n", i);
            printf("[key=%s]\n", ch->string);
            printf("----------------------\n");
            for(j = 0; j < kye_size; j++){
                printf("[value_num=%d          ]\n", j);
                printf("[key=%s]\n[value=%s]\n", key_ch->string, key_ch->valuestring); 
                key_ch = key_ch->next;
            }
            printf("----------------------\n");

        }else{
            printf("[num=%d          ]\n", i);
            printf("[key=%s]\n[value=%s]\n", ch->string, ch->valuestring);
        }
        ch = ch->next;
    }
}

编译执行命令:

代码语言:javascript
复制
gcc test.c cJSON.c -lm

运行结果:

image.png

参考

C - CJSON

CJSON 使用介绍

C语言cJSON库的使用,解析json数据格式

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
  • 实战举例
  • 代码实现
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档