JSON 下 -- jansson 示例

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

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<jansson.h>
  4 
  5 #define FILE_PATH         "./temp.txt"
  6 #define MAX_NUM            5
  7 
  8 typedef struct _JSON_ITEM_INFO
  9 {
 10     json_t* string;
 11     json_t* value;
 12 }JSON_ITEM_INFO;
 13 
 14 
 15 void save_info_to_file()
 16 {
 17    json_t* root = NULL;
 18    json_t* item_1 = NULL;
 19    json_t* item_2 = NULL;
 20    json_t* item_3 = NULL;
 21    json_t* array = NULL;
 22     
 23    char* s_repon = NULL;
 24    
 25    root = json_object();
 26    item_1 = json_object();
 27    item_2 = json_object();
 28    item_3 = json_object(); 
 29    array = json_array();
 30   
 31    json_object_set_new(item_1,"name",json_string("xiaopeng"));
 32    json_object_set_new(item_1,"age",json_integer(12));
 33    json_array_append_new(array,item_1);
 34 
 35    json_object_set_new(item_2,"name",json_string("xiaoming"));
 36    json_object_set_new(item_2,"age",json_integer(8));
 37    json_array_append_new(array,item_2);
 38 
 39    json_object_set_new(item_3,"name",json_string("xiaohong"));
 40    json_object_set_new(item_3,"age",json_integer(22));
 41    json_array_append_new(array,item_3);
 42    
 43    json_object_set_new(root,"root",array);
 44 
 45    json_dump_file(root, FILE_PATH,JSON_PRESERVE_ORDER);
 46 
 47    s_repon = json_dumps(root,JSON_INDENT(0));
 48 
 49    printf("s_repon = %s \n",s_repon);
 50    free(s_repon);
 51 
 52    printf("size = %d \n", (int)json_array_size(array));
 53    
 54    if(root)
 55    {
 56       json_delete(root);
 57    }
 58    if(array)
 59    {
 60       json_delete(array);
 61    }
 62 }
 63 
 64 void get_file_info()
 65 {
 66    int i = 0;
 67     
 68    json_t* root = NULL;
 69    json_t* array = NULL;
 70    json_error_t error;
 71    char* s_repon = NULL;
 72    
 73    json_t* add_item_1 = NULL;
 74    char* s_get_add_item = NULL;
 75    
 76    json_t* rec_table[MAX_NUM] = {0};
 77    
 78    JSON_ITEM_INFO person[MAX_NUM];
 79    memset(person,0,sizeof(person));
 80    
 81    //get the info from file;
 82    root = json_load_file(FILE_PATH, 0, &error);
 83    if(!json_is_object(root))
 84    {
 85         printf("%s,%d\n",__FILE__,__LINE__);
 86    }
 87    s_repon = json_dumps(root,JSON_INDENT(0));
 88    printf("s_repon = %s \n",s_repon);
 89    free(s_repon);
 90 
 91    array = json_object_get(root,"root");
 92    if(!json_is_array(array))
 93    {
 94         printf("%s,%d\n",__FILE__,__LINE__);
 95    }
 96    
 97    for(i = 0; i < MAX_NUM ;i++)
 98    {
 99        rec_table[i] = json_array_get(array,i);
100        if(!json_is_object(rec_table[i]))
101        {
102             printf("%s,%d\n",__FILE__,__LINE__);
103        }
104        person[i].string = json_object_get(rec_table[i],"name");
105        printf("person[%d].string = %s \n",i,json_string_value(person[i].string));
106        person[i].value = json_object_get(rec_table[i],"age");
107        printf("person[%d].value = %d \n",i,(int)json_integer_value(person[i].value));
108    }
109    
110     //add the new item;
111     add_item_1 = json_object(); 
112     json_object_set_new(add_item_1,"name",json_string("zhangsan"));
113     json_object_set_new(add_item_1,"age",json_integer(30));
114     
115    if(json_array_size(array) >= MAX_NUM)
116    {
117         //remove the top item;
118         json_array_remove(array,0);
119         
120    }    
121     json_array_append_new(array,add_item_1);
122 
123     //write the new array to the file;
124     json_dump_file(root, FILE_PATH,JSON_PRESERVE_ORDER);
125 
126     //dump the date and print
127     s_get_add_item = json_dumps(root,JSON_INDENT(0));
128 
129     printf("s_get_add_item = %s \n",s_get_add_item);
130     free(s_get_add_item);
131 
132 }
133 
134 int main()
135 {
136     save_info_to_file(); //这里将数据保存在文件 FILE_PATH 里;
137     get_file_info();     // 这里将文件 FILE_PATH 数据读取出来;
138 
139     return 0;
140 }

编译:

gcc source.c -ljansson

 

执行结果:

 

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

 

转载于:https://www.cnblogs.com/computer1-2-3/p/7247497.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值