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

cJSON库(构建json与解析json字符串)-c语言

发布时间:2020-12-16 19:39:34 所属栏目:百科 来源:网络整理
导读:工程中要先要有cJSONN库,两个文件分别是cJSON.c和cJSON.h。 下载地址:http://sourceforge.net/projects/cjson/ 例子1、解析JSON char * json = "{ "json" : { "id":1,"nodeId":11,"deviceId":111,"deviceName":"aaa","ieee":"01212","ep

工程中要先要有cJSONN库,两个文件分别是cJSON.c和cJSON.h。

下载地址:http://sourceforge.net/projects/cjson/

例子1、解析JSON

char * json = "{ "json" : { "id":1,"nodeId":11,"deviceId":111,"deviceName":"aaa","ieee":"01212","ep":"1111","type":"bbb" }}";  
char * json1 = "{"id":1,"deviceName":"aaa"}";  
cJSON * root;  
cJSON * format;  
int value_int;  
char * value_string;  
  
root = cJSON_Parse(json);   
format = cJSON_GetObjectItem(root,"json");     
value_int = cJSON_GetObjectItem(format,"nodeId")->valueint;   
value_string = cJSON_GetObjectItem(format,"ieee")->valuestring;   
printf( "%dn",value_int );  
printf( "%sn",value_string );  
cJSON_Delete(root);  
      
root = cJSON_Parse(json1);   
value_int = cJSON_GetObjectItem(root,"id")->valueint;   
value_string = cJSON_GetObjectItem(root,"deviceName")->valuestring;   
printf( "%dn",value_string );  
cJSON_Delete(root);
例子二:生成JSON数据

 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);


二、cJSON库

1、json的数据结构

c语言中json数据是采用链表存储的


typedef struct cJSON {   
    struct cJSON *next,*prev;// 数组 对象数据中用到   
    struct cJSON *child;// 数组 和对象中指向子数组对象或值   
    int type;// 元素的类型,如是对象还是数组   
    char *valuestring;// 如果是字符串   
    int valueint; // 如果是数值   
    double valuedouble;// 如果类型是cJSON_Number   
    char *string;// The item's name string,if this item is the child of,or is in the list of subitems of an object.   
} cJSON; 

三、cJSON使用
{   
    "name": "Jack ("Bee") Nimble","format": {   
        "type":       "rect","width":      1920,"height":     1080,"interlace":  false,"frame rate": 24   
    }   
} 
    "name": "Jack ("Bee") Nimble","format": {
        "type":       "rect","frame rate": 24
    }
}

1、字符串解析成json结构体

1):讲字符串解析成json结构体。

cJSON *root = cJSON_Parse(my_json_string);


2):获取某个元素

cJSON *format = cJSON_GetObjectItem(root,"format");

int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;

int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;


3):讲json结构体转换成字符串

char *rendered=cJSON_Print(root);


4):删除

cJSON_Delete(root);


2:构建一个json结构体

cJSON *root,*fmt;

root=cJSON_CreateObject();

cJSON_AddItemToObject(root,cJSON_CreateString("Jack ("Bee") Nimble"));

cJSON_AddItemToObject(root,"format",fmt=cJSON_CreateObject());

cJSON_AddStringToObject(fmt,"type","rect");

cJSON_AddNumberToObject(fmt,"width",1920);

cJSON_AddNumberToObject(fmt,"height",1080);

cJSON_AddFalseToObject (fmt,"interlace");

cJSON_AddNumberToObject(fmt,"frame rate",24)

out =cJSON_Print(root);

printf("%sn",out);

cJSON_Delete(root);

free(out);

(编辑:李大同)

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

    推荐文章
      热点阅读