cJSON很好使用的一个json解析器

cJSON:  一个用c写的一个简单好用的JSON解析器

 

下载地址: http://sourceforge.net/projects/cjson/files/?source=navbar

 

实例1: 创建一个简单的学生信息数组

 cJSON* pRoot = cJSON_CreateObject();
 cJSON* pArray = cJSON_CreateArray();
 cJSON_AddItemToObject(pRoot, "students_info", pArray);
 char* szOut = cJSON_Print(pRoot);

 cJSON* pItem = cJSON_CreateObject();
 cJSON_AddStringToObject(pItem, "name", "chenzhongjing");
 cJSON_AddStringToObject(pItem, "sex", "male");
 cJSON_AddNumberToObject(pItem, "age", 28);
 cJSON_AddItemToArray(pArray, pItem);

 pItem = cJSON_CreateObject();
 cJSON_AddStringToObject(pItem, "name", "fengxuan");
 cJSON_AddStringToObject(pItem, "sex", "male");
 cJSON_AddNumberToObject(pItem, "age", 24);
 cJSON_AddItemToArray(pArray, pItem);

 pItem = cJSON_CreateObject();
 cJSON_AddStringToObject(pItem, "name", "tuhui");
 cJSON_AddStringToObject(pItem, "sex", "male");
 cJSON_AddNumberToObject(pItem, "age", 22);
 cJSON_AddItemToArray(pArray, pItem);

 char* szJSON = cJSON_Print(pRoot);
 cJSON_Delete(pRoot);
 //free(szJSON);

 pRoot = cJSON_Parse(szJSON);
 pArray = cJSON_GetObjectItem(pRoot, "students_info");
 if (NULL == pArray)
 {
     return -1;
 }
 
 int iCount = cJSON_GetArraySize(pArray);
 for (int i = 0; i < iCount; ++i)
 {
     cJSON* pItem = cJSON_GetArrayItem(pArray, i);
     if (NULL == pItem)
     {
         continue;
     }

     string strName = cJSON_GetObjectItem(pItem, "name")->valuestring;
     string strSex = cJSON_GetObjectItem(pItem, "sex")->valuestring;
     int iAge = cJSON_GetObjectItem(pItem, "age")->valueint;
 }

 cJSON_Delete(pRoot);
 free(szJSON);

使用cjson解析一个json文件可以分为以下几步: 1. 引入cjson库的头文件,如:`#include "cJSON.h"` 2. 读取json文件内容到一个字符串变量中,如: ``` char* json_content = NULL; FILE* fp = fopen("example.json", "rb"); fseek(fp, 0, SEEK_END); long file_size = ftell(fp); rewind(fp); json_content = (char*)malloc(file_size + 1); fread(json_content, file_size, 1, fp); fclose(fp); json_content[file_size] = '\0'; ``` 3. 使用cJSON库的`cJSON_Parse()`函数解析json字符串,如: ``` cJSON* root = cJSON_Parse(json_content); if (root == NULL) { printf("Error before: [%s]\n", cJSON_GetErrorPtr()); } ``` 4. 通过`cJSON_GetObjectItem()`函数获取json对象的成员变量,如: ``` cJSON* name = cJSON_GetObjectItem(root, "name"); const char* name_value = name->valuestring; ``` 5. 释放cJSON对象的内存,如: ``` cJSON_Delete(root); ``` 完整的代码示例可以参考以下代码: ``` #include "cJSON.h" #include <stdio.h> #include <stdlib.h> int main() { char* json_content = NULL; FILE* fp = fopen("example.json", "rb"); fseek(fp, 0, SEEK_END); long file_size = ftell(fp); rewind(fp); json_content = (char*)malloc(file_size + 1); fread(json_content, file_size, 1, fp); fclose(fp); json_content[file_size] = '\0'; cJSON* root = cJSON_Parse(json_content); if (root == NULL) { printf("Error before: [%s]\n", cJSON_GetErrorPtr()); return 1; } cJSON* name = cJSON_GetObjectItem(root, "name"); const char* name_value = name->valuestring; printf("name: %s\n", name_value); cJSON_Delete(root); free(json_content); return 0; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值