github地址:  https://github.com/DaveGamble/cJSON

需要将cJSON.h 和 cJSON.c拷贝到路径下,并且连接所需库文件 -lm

步骤:
1、先将普通的json串处理成json对象,也就是所谓的创建json root的过程
char *js_string;
cJSON *root;
root = cJSON_Parse(js_string);
注意:
解析完成后,需释放
if(root)
    cJSON_Delete(root);
    
2、开始拿关键字,但如果关键字还有父层或者祖层,那就需要先从父层开拿,所谓剥洋葱是也!
先说没有父层的:
{"name": "xiaohong", "age": 10}
可以这样搞:
char *s = "{\"name\":\"xiao hong\",\"age\":10}";
cJSON *root = cJSON_Parse(s);

cJSON *name = cJSON_GetObjectItem(root, "name");
printf("name type is %d\n", name->type);
printf("name is %s\n", name->valuestring);

cJSON *age = cJSON_GetObjectItem(root, "age");
printf("age type is %d\n", age->type);
printf("age is %d\n", age->valueint);

再说有父层的:
{"list":{"name":"xiao hong",
         "age":10},
  "other":{"name":"hua hua"}}

char *s = "{\"list\":{\"name\":\"xiao hong\",\"age\":10},\"other\":{\"name\":\"hua hua\"}}";
cJSON *root = cJSON_Parse(s);
cJSON *js_list=cJSON_GetObjectItem(root, "list");
printf("list type is %d\n",js_list->type);
cJSON *name = cJSON_GetObjectItem(js_list, "name");
printf("name type is %d\n",name->type);
printf("name is %s\n",name->valuestring);
cJSON *age = cJSON_GetObjectItem(js_list, "age");
printf("age type is %d\n", age->type);
printf("age is %d\n",age->valueint);
cJSON *js_other = cJSON_GetObjectItem(root, "other");
printf("list type is %d\n",js_other->type);
cJSON *js_name = cJSON_GetObjectItem(js_other, "name");
printf("name type is %d\n",js_name->type);
printf("name is %s\n",js_name->valuestring);

if(root)
    cJSON_Delete(root);
return 0;

3、数组
char *s = "{\"list\":[\"name1\",\"name2\"]}";
cJSON *root = cJSON_Parse(s);
cJSON *js_list = cJSON_GetObjectItem(root, "list");
int array_size = cJSON_GetArraySize(js_list);
int i = 0;
cJSON *item;
for(i=0; i< array_size; i++) {
    item = cJSON_GetArrayItem(js_list, i);
    printf("item type is %d\n",item->type);
    printf("%s\n",item->valuestring);
}

---------------------------------------------
常用API(在文件cJSON.h中)
--解析json
1、从给定的json字符串中得到cjson对象
extern cJSON *cJSON_Parse(const char *value);

2、从json对象中获取有格式的json对象
extern char *cJSON_Print(cJSON *item);

3、从cjson中获取无格式的json对象
extern char *cJSON_PrintUnformatted(cJSON *item);

4、删除cjson对象,释放链表所占用的内存空间
extern void cJSON_Delete(cJSON *c);

5、获取cjson对象数组成员的个数
extern int cJSON_GetArraySize(cJSON *array);

6、根据下标获取cjson对象数组中的对象
extern cJSON_GetArrayItem(cJSON *array, int item);

7、根据键获取对应的值(cjson对象)
extern cJSON *cJSON_GetObjectItem(cJSON *object, const char *string);

8、获取错误字符串
extern const char *cJSON_GetErrorPtr(void);

--构造json
extern cJSON *cJSON_CreateObject(void);
extern void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
extern cJSON *cJSON_CreateNull(void);
extern cJSON *cJSON_CreatTrue(void);
extern cJSON *cJSON_createFalse(void);
extern cJSON *cJSON_CreateBool(int b);
extern cJSON *cJSON_CreateNumber(double num);
extern cJSON *cJSON_CreateString(const char *string);
extern cJSON *cJSON_CreateArray(void);
extern cJSON *cJSON_CreateObject(void);

最新文章

  1. Android——JDK的get请求方式
  2. 通过rpc访问比特币核心钱包
  3. java 实现二分查找法
  4. nRF51822之app_button控制uart的开启和关闭
  5. C# 控制连接超时
  6. jQuery 一些小技巧
  7. 101. Symmetric Tree
  8. (19)IO流之字符流FileReader和FileWriter,缓冲字符流---缓冲输入字符流BufferedReader和缓冲输出字符流BufferedWriter
  9. C# 中函数内定义函数的委托方法
  10. STM32F10XX存储器细节
  11. IDEA-最简单的struts2项目 关于lib项目的默认位置
  12. Angular服务的5种创建方式
  13. Mybatis逻辑分页原理解析RowBounds
  14. PLSQL表
  15. idea中经常用到的快捷键
  16. REST POST PUT差别
  17. unity的一些tips
  18. ISO-8859-1和GBK互转
  19. odoo开发笔记 -- odoo10 视图界面根据字段状态,动态隐藏创建&amp;编辑按钮
  20. php命令行按模板生成应用的入口文件

热门文章

  1. MessageBox页面消息弹出框类
  2. 【Python学习】Python3 基础语法
  3. 2.spring的主要模块作用
  4. 洛谷-P2661 信息传递——有向图中的最小环
  5. 3种方法实现CSS隐藏滚动条并可以滚动内容
  6. 运算符 &amp; | ^ ~ &gt;&gt; &lt;&lt; 讲解
  7. IDEA打开项目时,java文件左下角J图标的问题
  8. 2017.10.3 国庆清北 D3T2 公交车
  9. P2709 小B的询问 (莫队板子)
  10. Pytest权威教程17-安装和使用插件