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

 

发布了161 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42067873/article/details/104317920