JSON 下 -- jansson 示例
JSON 下 —— jansson 示例
参考网址:
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
执行结果:

有什么错漏,欢迎指出!!!
JSON 下 -- jansson 示例的更多相关文章
- Salesforce Apex 使用JSON数据的示例程序
本文介绍了一个在Salesforce Apex中使用JSON数据的示例程序, 该示例程序由以下几部分组成: 1) Album.cls, 定了了封装相关字段的数据Model类 2) RestClient ...
- JSON下
JSON下:目录一:把 JSON 文本转换为 JavaScript 对象二:JSON 实例 - 来自字符串的对象 一.把 JSON 文本转换为 JavaScript 对象JSON 最常见的用法之一,是 ...
- 运行所有sdk目录下的示例,查看它们的功能,方便以后查寻
运行所有sdk目录下的示例,查看它们的功能,方便以后查寻
- python中json的操作示例
先上一段示例 # -*- coding: cp936 -*- import json #构造一个示例数据,并打印成易读样式 j = {} j["userName"]="a ...
- linux 下jansson安装和使用
1.安装jansson ./configure make make install 2.生成帮助文档 cd doc make html 编译安装doc时提示 spinx-build not a com ...
- qt qml ajax 获取 json 天气数据示例
依赖ajax.js类库,以下代码很简单的实现了获取天气json数据并展示的任务 [TestAjax.qml] import QtQuick 2.0 import "ajax.js" ...
- Jquery Json 下拉联动
#region dataTable转换成Json格式 /// <summary> /// dataTable转换成Json格式 /// </summary> /// <p ...
- Jackson序列化和反序列化Json数据完整示例
Jackson序列化和反序列化Json数据 Web技术发展的今天,Json和XML已经成为了web数据的事实标准,然而这种格式化的数据手工解析又非常麻烦,软件工程界永远不缺少工具,每当有需求的时候就会 ...
- Json -- 语法和示例,javascript 解析Json
1. 语法 JSON(JavaScriptObject Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不 ...
随机推荐
- oracle sqlplus 常用操作
命令 含义 / 运行 SQL 缓冲区 ? [关键词] 对关键词提供 SQL 帮助 @[@] [文件名] [参数列表] 通过指定的参数,运行指定的命令文件 ACC[EPT] 变量 [DEF[AULT] ...
- HDU 5384 Danganronpa (2015年多校比赛第8场)
1.题目描写叙述:点击打开链接 2.解题思路:本题利用字典树解决.本题要求查找全部的B[j]在A[i]中出现的总次数.那么我们能够建立一颗字典树,将全部的B[j]插入字典树,因为一个串的全部字串相当于 ...
- vuex 介绍
vuex是为vue.js开发的状态管理模式,负责vue的状态管理,状态管理是干啥的呢,举个栗子,比如一个酒店,哪间屋子入住了客人,哪间屋子客人退房了,客人退房后,房间有没有清扫过,这些都需要去记录,以 ...
- 常用PHP MySQL函数
配置选项 MySQL 函数的行为受到 php.ini 中设置的影响. mysql.allow_persistent "1" 是否允许 MySQL 的持久连接. mysql.max_ ...
- 未开启HugePages ORACLE session剧增时引起的一次悲剧
故障简单描写叙述一下:LINUX系统未开启HugePages,主机内存将近300G.SWAP是32G.ORACLE 的 SGA_MAX_SIZE设置是主机内存的将近80%,SGA_TARGET设置是主 ...
- EasyDarwin云平台:EasyCamera开源摄像机接入海康威视摄像机PS流转ES流
本文转自EasyDarwin开源团队成员Alex的博客:http://blog.csdn.net/cai6811376 海康威视使用PS流封装H.264流,EasyDarwin云平台支持ES流.当我们 ...
- hsv hsb rgb lab
lab 欧式距离 反映 人类所能感受到的这两种颜色的差异
- Delphi类的默认区域
//类中数据成员的排列顺序一般是: 字段.方法.属性 //访问权限区域的顺序一般是: 无限制区(默认 published) private protected public pub ...
- Ubuntu下codeblocks编译器程序执行对话框内能进行粘贴编辑操作的指令
如这个: gnome-terminal -t $TITLE -x
- UVA11892 ENimEN —— 博弈
题目链接:https://vjudge.net/problem/UVA-11892 题意: 两人玩游戏,有n堆石子,每堆有ai块石子,两人轮流取,要求一次只能选择一堆石子取任意块.最后取完的获胜. 题 ...