cJSON库的使用(二)

一、C语言库函数解析json文件(已知json文件解析数据)

1、首先了解cJSON结构体的结构(双向链表实现)

/* The cJSON structure: */
typedef struct cJSON
{
    /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
    struct cJSON *next;//next,prev:双向链表的前后结点
    struct cJSON *prev;
    /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
    struct cJSON *child;

    /* The type of the item, as above. */
    int type;

    /* The item's string, if type==cJSON_String  and type == cJSON_Raw */
    char *valuestring;
    /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
    int valueint;
    /* The item's number, if type==cJSON_Number */
    double valuedouble;

    /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
    char *string;
} cJSON;

2、解析用的几个函数

1、获取json对象中的一个对象:
原型:extern cJSON* cJSON_GetObjectItem(cJSON *object, const char* string)
    cJSON* node = cJSON_GetObjectItem(json, "key");
2、判断json对象中是否含有某一个元素:
原型:extern int cJSON_HasObjectItem(cJSON *object, const char *string)
     {
         return cJSON_GetObjectItem(object, string) ? 1 : 0;
     }

    cJSON_HasObjectItem(json, "key");  //有:1; 无:0;
3、判断对象的value类型,参看cJSON结构体:
    node->type
4、获取数组元素的个数:
    extern int cJSON_GetArraySize(nodeArray);
5、根据数组下标,获取元素:
    extern cJSON* cJSON_GetArrayItem(nodeArr, index);
6、遍历json数组
原型:#define cJSON_ArrayForEach(pos, head) \
                for(pos = (head)->child; pos != NULL; pos = pos->next)
使用:cJSON_ArrayForEach(tmpNode, nodeArr)
    注:从nodeArr中取出一个元素放到tmpNode内

3、函数简单使用

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cJSON.h"
int main(void)
{
	char *string = "{\"family\": [\"father\", \"mother\", \"brother\", \"sister\"]}";
	//解析成json格式,存储在结构体中
	cJSON* json = cJSON_Parse(string);
	cJSON* node = NULL;

	//cJSON_GetObjectItem 根据key来查找json节点,如果有返回非空,否则返回空
	node = cJSON_GetObjectItem(json, "family");
	if(node == NULL){
		printf("family node == NULL\n");
	}else{
		printf("found family node\n");
	}

	//没有对应元素的情况
	node = cJSON_GetObjectItem(json, "famil");
	if(node == NULL){
		printf("-----famil node == NULL\n");
	}else{
		printf("-----found famil node\n");
	}

	if(1 == cJSON_HasObjectItem(json, "family")){
		printf("found family node\n");
	}else{
		printf("not found family node\n");
	}

	//cJSON_GetArraySize
	node = cJSON_GetObjectItem(json, "family");
	if(node->type == cJSON_Array){
		printf("array size is %d\n", cJSON_GetArraySize(node));
	}

	//非array类型的node 被当作array获取size的大小是未定义的  不要使用
	
	cJSON* tnode = NULL;
	int size = cJSON_GetArraySize(node);
	int i;

	//遍历
	for(i = 0; i < size; i++){
		tnode = cJSON_GetArrayItem(node, i);
		if(tnode->type == cJSON_String){
			printf("value[%d] : %s\n", i, tnode->valuestring);
		}else{
			printf("node's type is not string\n");
		}
	}

	cJSON_ArrayForEach(tnode, node){
		if(tnode->type == cJSON_String){
			printf("int ForEach: value : %s\n", tnode->valuestring);
		}else{
			printf("node's type is not string\n");
		}
	}
		
	return 0;
}

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是在Windows上编译和使用cjson的步骤: 1. 下载cjson的源代码,可以从官方网站上下载,或者从GitHub上下载。 2. 安装MinGW-w64,这是一个Windows平台下的GNU编译器套件,可以用于编译cjson。 3. 将MinGW-w64的安装路径添加到系统环境变量中,例如将C:\MinGW-w64\mingw64\bin添加到系统环境变量的Path变量中。 4. 打开命令提示符窗口,进入cjson源代码所在的目录。 5. 在命令提示符窗口中输入以下命令,编译cjson: ``` gcc -c cjson.c ar rcs libcjson.a cjson.o ``` 第一条命令是编译cjson.c文件,生成cjson.o目标文件;第条命令是将cjson.o目标文件打包成静态文件libcjson.a。 6. 编译完成后,在cjson源代码所在的目录下会生成libcjson.a静态文件。 7. 在需要使用cjson的代码中,包含cjson.h头文件,并链接libcjson.a静态文件。例如: ``` #include <stdio.h> #include "cjson.h" int main() { cJSON *root = cJSON_CreateObject(); cJSON_AddStringToObject(root, "name", "John"); char *json_str = cJSON_Print(root); printf("%s\n", json_str); cJSON_Delete(root); return 0; } ``` 在编译时,需要指定libcjson.a静态文件的路径和名称,例如: ``` gcc -o test test.c -L. -lcjson ``` 这里假设test.c是包含上述代码的文件,-L.指定静态文件的路径为当前目录,-lcjson指定要链接的静态文件为libcjson.a。 8. 运行编译后的可执行文件,即可使用cjson

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Star星屹程序设计

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值