JSON 下 —— jansson 示例

参考网址:

jansson 库的下载:

http://www.digip.org/jansson/

安装jansson 步骤:

http://blog.csdn.net/lz909/article/details/46042979

jansson 手册:

https://jansson.readthedocs.io/en/latest/index.html

API 介绍:

http://jansson.readthedocs.io/en/2.2/apiref.html#json_error_t

vim source.c

 #include<stdio.h>
#include<string.h>
#include<jansson.h> #define FILE_PATH "./temp.txt"
#define MAX_NUM 5 typedef struct _JSON_ITEM_INFO
{
json_t* string;
json_t* value;
}JSON_ITEM_INFO; void save_info_to_file()
{
json_t* root = NULL;
json_t* item_1 = NULL;
json_t* item_2 = NULL;
json_t* item_3 = NULL;
json_t* array = NULL; char* s_repon = NULL; root = json_object();
item_1 = json_object();
item_2 = json_object();
item_3 = json_object();
array = json_array(); json_object_set_new(item_1,"name",json_string("xiaopeng"));
json_object_set_new(item_1,"age",json_integer());
json_array_append_new(array,item_1); json_object_set_new(item_2,"name",json_string("xiaoming"));
json_object_set_new(item_2,"age",json_integer());
json_array_append_new(array,item_2); json_object_set_new(item_3,"name",json_string("xiaohong"));
json_object_set_new(item_3,"age",json_integer());
json_array_append_new(array,item_3); json_object_set_new(root,"root",array); json_dump_file(root, FILE_PATH,JSON_PRESERVE_ORDER); s_repon = json_dumps(root,JSON_INDENT()); printf("s_repon = %s \n",s_repon);
free(s_repon); printf("size = %d \n", (int)json_array_size(array)); if(root)
{
json_delete(root);
}
if(array)
{
json_delete(array);
}
} void get_file_info()
{
int i = ; json_t* root = NULL;
json_t* array = NULL;
json_error_t error;
char* s_repon = NULL; json_t* add_item_1 = NULL;
char* s_get_add_item = NULL; json_t* rec_table[MAX_NUM] = {}; JSON_ITEM_INFO person[MAX_NUM];
memset(person,,sizeof(person)); //get the info from file;
root = json_load_file(FILE_PATH, , &error);
if(!json_is_object(root))
{
printf("%s,%d\n",__FILE__,__LINE__);
}
s_repon = json_dumps(root,JSON_INDENT());
printf("s_repon = %s \n",s_repon);
free(s_repon); array = json_object_get(root,"root");
if(!json_is_array(array))
{
printf("%s,%d\n",__FILE__,__LINE__);
} for(i = ; i < MAX_NUM ;i++)
{
rec_table[i] = json_array_get(array,i);
if(!json_is_object(rec_table[i]))
{
printf("%s,%d\n",__FILE__,__LINE__);
}
person[i].string = json_object_get(rec_table[i],"name");
printf("person[%d].string = %s \n",i,json_string_value(person[i].string));
person[i].value = json_object_get(rec_table[i],"age");
printf("person[%d].value = %d \n",i,(int)json_integer_value(person[i].value));
} //add the new item;
add_item_1 = json_object();
json_object_set_new(add_item_1,"name",json_string("zhangsan"));
json_object_set_new(add_item_1,"age",json_integer()); if(json_array_size(array) >= MAX_NUM)
{
//remove the top item;
json_array_remove(array,); }
json_array_append_new(array,add_item_1); //write the new array to the file;
json_dump_file(root, FILE_PATH,JSON_PRESERVE_ORDER); //dump the date and print
s_get_add_item = json_dumps(root,JSON_INDENT()); printf("s_get_add_item = %s \n",s_get_add_item);
free(s_get_add_item); } int main()
{
save_info_to_file(); //这里将数据保存在文件 FILE_PATH 里;
get_file_info(); // 这里将文件 FILE_PATH 数据读取出来; return ;
}

编译:

gcc source.c -ljansson

执行结果:

有什么错漏,欢迎指出!!!

最新文章

  1. windows 下的sleep 命令
  2. eclipse技巧总结
  3. Remove Element
  4. javaSE第五天
  5. [置顶] 两台一级域名相同二级域名不同的服务器,怎么共享session
  6. XAMPP(v1.83)中的PHP(v5.5.15)访问SQLServer2014
  7. List集合数据太多进行分批,List的subList方法应用
  8. Android listView异步下载和convertView复用产生的错位问题
  9. Python 函数式编程和面向对象编程
  10. authorizations.go
  11. LeetCode算法题-Binary Watch(Java实现)
  12. vue教程2-07 微博评论功能
  13. Double-Array Trie分词词典简述
  14. js正则表达式实现手机号码,密码正则验证
  15. jmeter正则表达式提取器--关联
  16. 利用SHELL的PROMPT_COMMAND添加日志审计功能,实时记录任何用户的操作到日志文件中
  17. Storm学习笔记——高级篇
  18. SimpleDateFormat 的 format 方法使用具体解释
  19. 数据库、Java与Hibernate数据类型对照
  20. (PHP)redis String(字符串)操作

热门文章

  1. uboot下载地址
  2. vue directive demo
  3. Spring LDAP
  4. Windows8-x64 VMWare安装Linux CentOS6-x64
  5. EasyPlayer开源流媒体移动端播放器推出RTSP-RTMP-HTTP-HLS全功能Pro版
  6. EasyPusher:基于live555的DarwinInjector实现的RTSP直播推送程序
  7. 九度OJ 1122:吃糖果 (递归)
  8. 关于myeclipse缓存问题
  9. Impala 安装笔记3一impala安装
  10. GCD 开启一个定时器实现倒计时功能