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数据不 ...
随机推荐
- 【SSH2(理论篇)】--Struts2配置具体解释
上篇博客讨论了SSH2框架模型,在开发过程中发现SSH2的开发模型事实上类似于经典的三层模式,在每一层中分别加入了不同的框架,显示层使用的是Struts2进行配置的,业务逻辑层使用的是Spring配置 ...
- selector模块使用
#服务端 from socket import * import selectors sel=selectors.DefaultSelector() def accept(server_fileobj ...
- maven的坑2
导入工程后,pom.xml文件中以下插件报错: <plugin> <groupId>com.jayway.maven.plugins.android.generation2&l ...
- android handler looper
http://www.cnblogs.com/plokmju/p/android_Handler.html
- RTSP/RTMP/HLS/HTTP流媒体播放器EasyPlayer
EasyPlayer播放器系列项目 EasyPlayer是由EasyDarwin开源团队开发和维护的一个流媒体播放器系列项目,随着多年不断的发展和迭代,不断基于成功的实践经验,发展出包括有: Easy ...
- Hibernate中的Sesson操作
一.Session概述 Session是应用程序与数据库之间的一个会话,是Hibernate运作的中心,持久层操作的基础,相当于JDBC中的Connection.Session对象是通过Session ...
- arm处理器的历史及现状
1 arm处理器的发展历史 arm1 arm2 arm3 arm6 arm7 arm9 arm11 arm cortex 2 arm处理器现状 arm cortex A a即application,即 ...
- r squared
multiple r squared adjusted r squared http://web.maths.unsw.edu.au/~adelle/Garvan/Assays/GoodnessOfF ...
- 【Effective C++】资源管理
资源:动态分配的内存.文件描述器.互斥锁.图形界面中的字型与笔刷.数据库连接以及网络sockets等,无论哪一种资源,重要的是,当你不再使用它时,必须将它还给系统. 条款13:以对象管理资源 当我们向 ...
- 关于<context:annotation-config/>配置
对于spring项目的一些配置,一直感到有些混乱,今天看到一前辈总结的特别好,把自己的理解贴在这里,有不当的地方,后续继续学习: 当我们使用@Autowired.@Required等这些注解时,就要在 ...