cJONS序列化工具解读三(使用案例)
2016-05-19 09:24
453 查看
cJSON使用案例
由了解了cJSON的数据结构,接口以及实现之后,那么我们来举例说明其使用。
本例子是一个简单的学生信息表格管理,我们通过键值对的方式向json中增加元素信息。
然后可以格式化输出结果,也能够反向的由字符串输出生成原cJSON对象。
亦或是对于格式化的字符串,交给cJSON管理,自动识别,产生类型输出。
由了解了cJSON的数据结构,接口以及实现之后,那么我们来举例说明其使用。
本例子是一个简单的学生信息表格管理,我们通过键值对的方式向json中增加元素信息。
然后可以格式化输出结果,也能够反向的由字符串输出生成原cJSON对象。
int Test_cJSON() { 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); cout << szJSON << endl; 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管理,自动识别,产生类型输出。
/* Parse text to JSON, then render back to text, and print! */ void doit(char *text) { char *out; cJSON *json; json = cJSON_Parse(text); if (!json) { printf("Error before: [%s]\n", cJSON_GetErrorPtr()); } else { out = cJSON_Print(json); cJSON_Delete(json); printf("%s\n", out); free(out); } } /* Read a file, parse, render back, etc. */ void dofile(char *filename) { FILE *f; long len; char *data; f = fopen(filename, "rb"); assert(f); fseek(f, 0, SEEK_END); len = ftell(f); fseek(f, 0, SEEK_SET); data = (char*)malloc(len + 1); memset(data, 0, sizeof(char)*(len + 1)); fread(data, 1, len, f); printf("%s", data); fclose(f); doit(data); free(data); }
int main() { char text1[] = "{\n\"name\": \"Jack (\\\"Bee\\\") Nimble\", \n\"format\": {\"type\": \"rect\", \n\"width\": 1920, \n\"height\": 1080, \n\"interlace\": false,\"frame rate\": 24\n}\n}"; doit(text1); return 0;
}
相关文章推荐
- php script 的生命周期
- Flex xml格式化以及缩进量
- Android Studio如何查看branch列表及切换branch
- iOS学习之—— NSUserDefaults
- YOLO
- Pod经常冲突 拉下来缺少库
- 第11周项目3—警察和厨师 (2)
- Smart210---学习记录 竞态与并发
- 获取IWebBrowser2指针的方法(二)
- 获取IWebBrowser2指针的方法(一)
- 性能测试常见用语
- Google开源的FlexboxLayout
- Mac上Matlab库libsvm安装指南
- 聊聊UNDO_RETENTION作用(下)
- freemarker数字格式化引入的问题
- HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误
- 第12周阅读教程(4)
- 项目2-职员有薪水了(2)(第10,11周补充项目 继承和派生)
- js获取显示器分辨率
- python中日期和时间格式化输出