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

一、c语言获取json中的数据。

1、先要有cJOSN库,两个文件分别是cJSON.c和cJSON.h。


2、感性认识

[cpp]  view plain copy
  1.   
[cpp]  view plain copy
  1.   
[cpp]  view plain copy
  1. <span style="font-family:Courier New;font-size:12px;">char * json = "{ \"json\" : { \"id\":1, \"nodeId\":11, \"deviceId\":111, \"deviceName\":\"aaa\", \"ieee\":\"01212\", \"ep\":\"1111\", \"type\":\"bbb\" }}";  
  2. char * json1 = "{\"id\":1, \"nodeId\":11, \"deviceId\":111, \"deviceName\":\"aaa\"}";  
  3. cJSON * root;  
  4. cJSON * format;  
  5. int value_int;  
  6. char * value_string;  
  7.   
  8. root = cJSON_Parse(json);   
  9. format = cJSON_GetObjectItem(root,"json");     
  10. value_int = cJSON_GetObjectItem(format,"nodeId")->valueint;   
  11. value_string = cJSON_GetObjectItem(format,"ieee")->valuestring;   
  12. printf( "%d\n", value_int );  
  13. printf( "%s\n", value_string );  
  14. cJSON_Delete(root);  
  15.       
  16. root = cJSON_Parse(json1);   
  17. value_int = cJSON_GetObjectItem(root,"id")->valueint;   
  18. value_string = cJSON_GetObjectItem(root,"deviceName")->valuestring;   
  19. printf( "%d\n", value_int );  
  20. printf( "%s\n", value_string );  
  21. cJSON_Delete(root);</span>  
[cpp]  view plain copy
  1.   
[cpp]  view plain copy
  1.   
[cpp]  view plain copy
  1.   

结果:

[cpp]  view plain copy
  1. <span style="font-family:Courier New;font-size:12px;">11  
  2. 01212  
  3. 1  
  4. aaa  
  5. </span>  

二、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", 

        "width":      1920, 

        "height":     1080, 

        "interlace":  false, 

        "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, "name", 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("%s\n",out); 

cJSON_Delete(root);

free(out);




原文地址:http://blog.csdn.net/cy_cai/article/details/9821473


  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值