加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:54:13 所属栏目:百科 来源:网络整理
导读:cJSON: 一个用c写的一个简单好用的JSON解析器 下载地址: http://sourceforge.net/projects/cjson/files/?source=navbar 实例1: 创建一个简单的学生信息数组 cJSON* pRoot = cJSON_CreateObject(); cJSON* pArray = cJSON_CreateArray(); cJSON_AddItemToObje

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,"fengxuan");
cJSON_AddStringToObject(pItem,24);
cJSON_AddItemToArray(pArray,"tuhui");
cJSON_AddStringToObject(pItem,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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读