使用Cjson在C语言进行Json的创建和解析
2016-10-11 13:44
465 查看
1,创建json,从json中获取数据。
运行结果:
// 创建的JSon字符串
{
“hello”: “hello world”,
“number”: 10010,
“bool”: true,
“subobj”: {
“subjsonobj”: “a sub json string”
}
}
// 从Json解析得到的数据
obj_1 : hello world
obj_2 : 10010
obj_3 : 1
sub_obj_1 : a sub json string
2,创建json数组和解析json数组。
//CJSON在内存中的存储方式是用链表进行存储的,所以在进行操作的时候,我们可见的部分全部是用指针进行操作的。 #include <stdio.h> #include "cJSON.h" char * makeJson() { cJSON * pJsonRoot = NULL; pJsonRoot = cJSON_CreateObject(); //新建一个JSON项目:pSubJson if(NULL == pJsonRoot) { //error happend here return NULL; } //add 字符串、数字和bool变量 cJSON_AddStringToObject(pJsonRoot, "hello", "hello world"); cJSON_AddNumberToObject(pJsonRoot, "number", 10010); cJSON_AddBoolToObject(pJsonRoot, "bool", 1); cJSON * pSubJson = NULL; //新建一个JSON项目:pSubJson。 pSubJson = cJSON_CreateObject(); if(NULL == pSubJson) { // create object faild, exit cJSON_Delete(pJsonRoot); return NULL; } // pSubJson项目上添加字符串。 cJSON_AddStringToObject(pSubJson, "subjsonobj", "a sub json string"); // 新项目添加到最初的项目pJsonRoot上 cJSON_AddItemToObject(pJsonRoot, "subobj", pSubJson); char * p = cJSON_Print(pJsonRoot); // else use : // char * p = cJSON_PrintUnformatted(pJsonRoot); if(NULL == p) { //convert json list to string faild, exit //because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot, if you also delete pSubJson, it will coredump, and error is : double free cJSON_Delete(pJsonRoot); return NULL; } //free(p); cJSON_Delete(pJsonRoot); return p; } void parseJson(char * pMsg) { if(NULL == pMsg) { return; } cJSON * pJson = cJSON_Parse(pMsg); if(NULL == pJson) { // parse faild, return return ; } // get string from json cJSON * pSub = cJSON_GetObjectItem(pJson, "hello"); if(NULL == pSub) { //get object named "hello" faild } printf("obj_1 : %s\n", pSub->valuestring); // get number from json pSub = cJSON_GetObjectItem(pJson, "number"); if(NULL == pSub) { //get number from json faild } printf("obj_2 : %d\n", pSub->valueint); // get bool from json pSub = cJSON_GetObjectItem(pJson, "bool"); if(NULL == pSub) { // get bool from json faild } printf("obj_3 : %d\n", pSub->valueint); // get sub object pSub = cJSON_GetObjectItem(pJson, "subobj"); if(NULL == pSub) { // get sub object faild } cJSON * pSubSub = cJSON_GetObjectItem(pSub, "subjsonobj"); if(NULL == pSubSub) { // get object from subject object faild } printf("sub_obj_1 : %s\n", pSubSub->valuestring); cJSON_Delete(pJson); } int main() { char * p = makeJson(); if(NULL == p) { return 0; } printf("%s\n", p); parseJson(p); free(p); //千万不要忘记释放内存呀,cJSON_Print()函数或者cJSON_PrintUnformatted()产生的内存,使用free(char *)进行释放 return 0; }
运行结果:
// 创建的JSon字符串
{
“hello”: “hello world”,
“number”: 10010,
“bool”: true,
“subobj”: {
“subjsonobj”: “a sub json string”
}
}
// 从Json解析得到的数据
obj_1 : hello world
obj_2 : 10010
obj_3 : 1
sub_obj_1 : a sub json string
2,创建json数组和解析json数组。
//创建数组,数组值是另一个JSON的item,这里使用数字作为演示 char * makeArray(int iSize) { cJSON * root = cJSON_CreateArray(); if(NULL == root) { printf("create json array faild\n"); return NULL; } int i = 0; for(i = 0; i < iSize; i++) { cJSON_AddNumberToObject(root, "hehe", i); } char * out = cJSON_Print(root); cJSON_Delete(root); return out; } //解析刚刚的CJSON数组 void parseArray(char * pJson) { if(NULL == pJson) { return ; } cJSON * root = NULL; if((root = cJSON_Parse(pJson)) == NULL) { return ; } int iSize = cJSON_GetArraySize(root); for(int iCnt = 0; iCnt < iSize; iCnt++) { cJSON * pSub = cJSON_GetArrayItem(root, iCnt); if(NULL == pSub) { continue; } int iValue = pSub->valueint; printf("value[%2d] : [%d]\n", iCnt, iValue); } cJSON_Delete(root); return; }
相关文章推荐
- 使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解
- 使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解
- 使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解
- (转)使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解
- 使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解
- 在不知道json格式的情况下如何使用cjson进行解析
- 有关使用cJSON包解析或者创建JSON时遇到的错误和解决办法
- 在不知道json格式的情况下如何使用cjson进行解析
- 全面详解c语言使用cJSON解析JSON字符
- C语言中进行 JSON 的创建和解析
- 在不知道json格式的情况下如何使用cjson进行解析
- C语言创建及解析Json的使用法则
- 如何使用rapidjson库进行json格式的封装及解析
- 使用 Swift 进行 JSON 解析
- 解析Json数据:使用Gson进行解析json数据<二>
- 使用com.google.gson包进行json字符串的构造与解析
- 解析Json数据:使用FastJson进行解析json数据<三>
- iOS 使用NSJSONSerialization类进行json解析
- 使用JSONObject和JSONArray 进行Json解析
- springMVC使用ajax返回json对象,用jquery进行解析.