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


    转自:http://blog.csdn.net/chenzhongjing/article/details/9188347

    下载地址: 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);
  • 相关阅读:
    【Beta阶段】第一次Scrum Meeting
    【Beta阶段】第二次Scrum Meeting
    【Beta阶段】第三次Scrum Meeting
    [BUAA软工]Alpha阶段事后分析
    [BUAA软工]Alpha阶段测试报告
    [北航软工]团队贡献分规则
    Windows Server 2008 R2之二从介质安装 AD DS
    Windows Server 2008 R2之一活动目录服务部署
    DC84问
    获取命令行指定参数
  • 原文地址:https://www.cnblogs.com/x_wukong/p/4543997.html
Copyright © 2020-2023  润新知