json-c开发指南

1、json格式介绍

	JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。

2、json键值类型介绍

	json_type_null,
	json_type_boolean
	json_type_double,
	json_type_int, 
	json_type_object,
	json_type_array,
	json_type_string

3. 解析json

3.1 解析键值为json_type_null型示例

	char *buf = "{\"key\":null}";
	json_object  *root0 = json_tokener_parse();
	json_object  *root1; 
	if(json_object_object_get_ex(root0 ,"key",&root1 )){
		if(json_object_get_type(book) != json_type_null){
			printf("\njson_object_get_type(book) != json_type_null \n");
		}
		else{
			printf("\njson_object_get_type(book) == json_type_null \n");
		}
	}
	json_object_put(root0);

3.2 解析键值为json_type_boolean型示例

	char *buf = "{\"key\":true}";
	json_object  *root0 = json_tokener_parse();
	json_object  *root1; 
	if(json_object_object_get_ex(root0 ,"key",&root1 )){
		printf("\n key = %d\n",json_object_get_boolean(root1));
	}
	json_object_put(root0);

3.3 解析键值为json_type_double型示例

	char *buf = "{\"key\":1.0}";
	json_object  *root0 = json_tokener_parse();
	json_object  *root1; 
	if(json_object_object_get_ex(root0 ,"key",&root1 )){
		printf("\n key = %f\n",json_object_get_double(&root1));
	}
	json_object_put(root0);

3.4 解析键值为json_type_int型示例

	char *buf = "{\"key\":1}";
	json_object  *root0 = json_tokener_parse();
	json_object  *root1; 
	if(json_object_object_get_ex(root0 ,"key",&root1 )){
		printf("\n key = %d\n",json_object_get_int(&root1));
	}
	json_object_put(root0);

3.5 解析键值为json_type_object型示例

	char *buf = "{\"key\":{\"key1\":\"hello\"}}";
	json_object  *root0 = json_tokener_parse();
	json_object  *root1; 
	if(json_object_object_get_ex(root0 ,"key",&root1 )){
		
	}
	json_object_put(root0);
	注:对于json_type_object类型来说,是下一级的json

3.6 解析键值为json_type_array型示例

	char *buf = "{\"key\":[1,8,9]}";
	json_object  *root0 = json_tokener_parse();
	json_object  *root1,*root2; 
	if(json_object_object_get_ex(root0 ,"key",&root1 )){
		root2 = json_object_array_get_idx(root1 ,0);
		printf("\n value = %d\n", json_object_get_int(root2));
	}
	json_object_put(root0);

3.7 解析键值为json_type_string型示例

	char *buf = "{\"key\":\"hello\"}}";
	json_object  *root0 = json_tokener_parse();
	json_object  *root1; 
	if(json_object_object_get_ex(root0 ,"key",&root1 )){
		printf("\n key = %d\n",json_object_get_string(&root1));
	}
	json_object_put(root0);

4.封装json

4.1 封装键值为json_type_null型示例

	json_object *root = json_object_new_object();
	json_object_object_add(root, "key",NULL);  
	printf("\n root = %s\n",json_object_get_string(root));
	json_put_object(root);

4.2 封装键值为json_type_boolean型示例

	json_object *root = json_object_new_object();
	json_object_object_add(root, "key",json_object_new_boolean(1));  
	printf("\n root = %s\n",json_object_get_string(root));
	json_put_object(root);

4.3 封装键值为json_type_double型示例

	json_object *root = json_object_new_object();
	json_object_object_add(root, "key",json_object_new_double(1.0));  
	printf("\n root = %s\n",json_object_get_string(root));
	json_put_object(root);

4.4 封装键值为json_type_int型示例

	json_object *root = json_object_new_object();
	json_object_object_add(root, "key",json_object_new_int(10));  
	printf("\n root = %s\n",json_object_get_string(root));
	json_put_object(root);

4.5 封装键值为json_type_object型示例

	json_object *root = json_object_new_object();
	json_object *root1 = json_object_new_object();
	json_object_object_add(root, "key",root1);  
	printf("\n root = %s\n",json_object_get_string(root));
	json_put_object(root);

4.6 封装键值为json_type_array型示例

	json_object *root = json_object_new_object();
    json_object *root1 = json_object_new_array();
	json_object_object_add(root, "key",root1 );  
	printf("\n root = %s\n",json_object_get_string(root1));
	json_put_object(root);

4.7 封装键值为json_type_string型示例

	json_object *root = json_object_new_object();
	json_object_object_add(root, "key",json_object_new_string("123"));  
	printf("\n root = %s\n",json_object_get_string(root1));
	json_put_object(root);

5、部分api说明

	json_object_object_foreach(obj,key,val) 遍历json所有的key
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值