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

前端之家收集整理的这篇文章主要介绍了cJSON库(构建json与解析json字符串)-c语言前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

@H_502_15@

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

@H_502_15@

2、感性认识

@H_502_15@
  1. char*json={\"json\":{\"id\":1,\"nodeId\":11,\"deviceId\":111,\"deviceName\":\"aaa\",\"ieee\":\"01212\",\"ep\":\"1111\",\"type\":\"bbb\"}}";
  2. char*json1="{\"id\":1,\"deviceName\":\"aaa\"}";
  3. cJSON*root;
  4. cJSON*format;
  5. intvalue_int;
  6. char*value_string;
  7. root=cJSON_Parse(json);
  8. format=cJSON_GetObjectItem(root,"json");
  9. value_int=cJSON_GetObjectItem(format,"nodeId")->valueint;
  10. value_string=cJSON_GetObjectItem(format,"ieee")->valuestring;
  11. printf("%d\n",value_int);
  12. printf("%s\n",value_string);
  13. cJSON_Delete(root);
  14. root=cJSON_Parse(json1);
  15. value_int=cJSON_GetObjectItem(root,"id")->valueint;
  16. value_string=cJSON_GetObjectItem(root,"deviceName")->valuestring;
  17. printf("%d\n",value_int);
  18. printf("%s\n",value_string);
  19. cJSON_Delete(root);

@H_502_15@

@H_502_15@

结果:

    11
  1. 01212
  2. 1
  3. aaa

二、cJSON库

@H_502_15@

1、json的数据结构

@H_502_15@

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

@H_502_15@

typedef struct cJSON {

@H_502_15@

struct cJSON *next,*prev;// 数组 对象数据中用到

@H_502_15@

struct cJSON *child;// 数组 和对象中指向子数组对象或值

@H_502_15@

int type;// 元素的类型,如是对象还是数组

@H_502_15@

char *valuestring;// 如果是字符串

@H_502_15@

int valueint;// 如果是数值

@H_502_15@

double valuedouble;// 如果类型是cJSON_Number

@H_502_15@

char *string;// The item's name string,if this item is the child of,or is in the list of subitems of an object.

@H_502_15@

} cJSON;

三、cJSON使用

@H_502_15@

{

@H_502_15@

"name": "Jack (\"Bee\") Nimble",

@H_502_15@

"format": {

@H_502_15@

"type": "rect",

@H_502_15@

"width": 1920,

@H_502_15@

"height": 1080,

@H_502_15@

"interlace": false,

@H_502_15@

"frame rate": 24

@H_502_15@

}

@H_502_15@

}

@H_502_15@

"name": "Jack (\"Bee\") Nimble",

@H_502_15@

"format": {

@H_502_15@

"type": "rect",

@H_502_15@

"width": 1920,

@H_502_15@

"height": 1080,

@H_502_15@

"interlace": false,

@H_502_15@

"frame rate": 24

@H_502_15@

}

@H_502_15@

}

@H_502_15@

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

@H_502_15@

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

@H_502_15@

cJSON *root = cJSON_Parse(my_json_string);

@H_502_15@

2):获取某个元素

@H_502_15@

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

@H_502_15@

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

@H_502_15@

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

@H_502_15@

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

@H_502_15@

char *rendered=cJSON_Print(root);

@H_502_15@

4):删除

@H_502_15@

cJSON_Delete(root);

@H_502_15@

2:构建一个json结构体

@H_502_15@

cJSON *root,*fmt;

@H_502_15@

root=cJSON_CreateObject();

@H_502_15@

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

@H_502_15@

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

@H_502_15@

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

@H_502_15@

cJSON_AddNumberToObject(fmt,"width",1920);

@H_502_15@

cJSON_AddNumberToObject(fmt,"height",1080);

@H_502_15@

cJSON_AddFalseToObject (fmt,"interlace");

@H_502_15@

cJSON_AddNumberToObject(fmt,"frame rate",24)

@H_502_15@

out =cJSON_Print(root);

@H_502_15@

printf("%s\n",out);

@H_502_15@

cJSON_Delete(root);

@H_502_15@

free(out);

猜你在找的Json相关文章