JSON 下 -- jansson 樣本

來源:互聯網
上載者:User

標籤:file_path   檔案   code   取出   and   樣本   tail   分享   min   

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 print127     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

 

執行結果:

 

 有什麼錯漏,歡迎指出!!!

 

JSON 下 -- jansson 樣本

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.