cJSON:封装与解析(二)

该篇介绍使用 cJSON 封装和解析数组。cJSON的简单使用请看这篇

测试字符串:

{
    "name":	"children info",
    "info":	
    [
        {
            "name":	"lilei",
            "age":	5,
            "height":	0.45
        }, 
        {
            "name":	"lily",
            "age":	4,
            "height":	0.35
        }, 
        {
            "name":	"hanmeimei",
            "age":	4,
            "height":	0.4
        }
    ]
}

1. 封装

enum json_print_formate
{
	PRINT_FORMATE = 0,    /*按 json 格式输出*/
	PRINT_UNFORMATE,      /*以字符串形式输出*/
};

char *json_array_package(enum json_print_formate flag)
{
    cJSON *root = NULL; 
    cJSON *info = NULL;
    cJSON *child_info = NULL;
    char *out = NULL;

    root = cJSON_CreateObject(); /*创建 json 根节点*/

    cJSON_AddStringToObject(root, "name", "children info");  /*添加字符串*/
    info = cJSON_CreateArray(); /*创建 json 数组*/
    cJSON_AddItemToObject(root, "info", info);    /*将数组加入到根节点*/
    
    child_info = cJSON_CreateObject();    /*向数组中添加成员*/
    cJSON_AddItemToArray(info, child_info);
    cJSON_AddStringToObject(child_info, "name", "lilei");  /*添加字符串*/
    cJSON_AddNumberToObject(child_info, "age", 5);      /*添加整型*/
    cJSON_AddNumberToObject(child_info, "height", 0.45);  /*添加浮点型*/

    child_info = cJSON_CreateObject();    /*向数组中添加成员*/
    cJSON_AddItemToArray(info, child_info);
    cJSON_AddStringToObject(child_info, "name", "lily");  /*添加字符串*/
    cJSON_AddNumberToObject(child_info, "age", 4);      /*添加整型*/
    cJSON_AddNumberToObject(child_info, "height", 0.35);  /*添加浮点型*/

    child_info = cJSON_CreateObject();    /*向数组中添加成员*/
    cJSON_AddItemToArray(info, child_info);
    cJSON_AddStringToObject(child_info, "name", "hanmeimei");  /*添加字符串*/
    cJSON_AddNumberToObject(child_info, "age", 4);      /*添加整型*/
    cJSON_AddNumberToObject(child_info, "height", 0.40);  /*添加浮点型*/
    
    if(PRINT_FORMATE == flag)
    {
        out = cJSON_Print(root);   
    }
    else
    {
        out = cJSON_PrintUnformatted(root);    /*将json形式打印成正常字符串形式*/
    }

    /*释放内存*/  
    cJSON_Delete(root);     

    return out;  
}

2. 解析

void json_array_parse(char *json_string)
{
    cJSON *root = NULL;    /*json 根节点*/
    cJSON *name = NULL;
    cJSON *jarray = NULL;    /*json 数组*/
    cJSON *member = NULL;
    cJSON *age = NULL;
    cJSON *height = NULL;

    root = cJSON_Parse(json_string);
    name = cJSON_GetObjectItem( root , "name" );
    printf("root name:%s\n", name->valuestring);
    
    jarray = cJSON_GetObjectItem(root, "info");
    cJSON_ArrayForEach(member, jarray)    /*遍历数组*/
    {
        name = cJSON_GetObjectItem( member , "name" );
        age = cJSON_GetObjectItem( member , "age" );
        height = cJSON_GetObjectItem( member , "height" );
        printf("name:%s, age:%d, height:%f\n", name->valuestring, age->valueint, height->valuedouble);
    }

    /*释放内存*/  
    cJSON_Delete(root);     
}

3. 测试

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"cJSON.h"

int main()
{
    char *json_str = NULL;

    json_str = json_array_package(PRINT_FORMATE);
    printf("array formate:\n%s\n", json_str);
    json_array_parse(json_str);
    cJSON_free(json_str);

    json_str = json_array_package(PRINT_UNFORMATE);
    printf("\narray unformate:\n%s\n", json_str);
    json_array_parse(json_str);
    cJSON_free(json_str);

}

   CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
 
project(json_test)
 
include_directories(./)
 
aux_source_directory(./ SRC_FILES)
 
add_executable(${PROJECT_NAME} ${SRC_FILES})

4. 结果

array formate:
{
	"name":	"children info",
	"info":	[{
			"name":	"lilei",
			"age":	5,
			"height":	0.45
		}, {
			"name":	"lily",
			"age":	4,
			"height":	0.35
		}, {
			"name":	"hanmeimei",
			"age":	4,
			"height":	0.4
		}]
}
root name:children info
name:lilei, age:5, height:0.450000
name:lily, age:4, height:0.350000
name:hanmeimei, age:4, height:0.400000

array unformate:
{"name":"children info","info":[{"name":"lilei","age":5,"height":0.45},{"name":"lily","age":4,"height":0.35},{"name":"hanmeimei","age":4,"height":0.4}]}
root name:children info
name:lilei, age:5, height:0.450000
name:lily, age:4, height:0.350000
name:hanmeimei, age:4, height:0.400000

Note:

cJSON_Print(const cJSON *item) 和 cJSON_PrintUnformatted(const cJSON *item) 这两个函数会调用 malloc 分配内存,需要调用 cJSON_free(void *object) 进行释放。

上一篇

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值