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数据不 ...
随机推荐
- AngularJs + html 5 实现 裁剪上传
直接上代码 directive.js app.directive('fileUploadersm', function () { return { restrict: 'E', transclude: ...
- openh264 在 osx 上的 nasm 问题
先在 pc 上编译,熟悉一下. 编译遇到一个问题: nasm -DUNIX64 -DPREFIX -f macho64 -I./codec/common/x86/ -o codec/common/x8 ...
- nginx 安装和配置文件说明
1. 安装依赖包 yum install gcc gcc+ yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-devel 2 ...
- 05 referer头与防盗链
像上图中的这个效果,当我们在网页里引用站外图片时,常出现这样的情况. ??? 服务器是怎么样知道,这个图片是在站外被引用的呢? 还有在网站的统计结果,统计用户从何而来,如下图 ??? 统计时,是如何得 ...
- VMware虚拟机下实现Linux与window文件夹共享
这里说的是在VMware虚拟机下来实现在windows与Linux下共享一个文件夹. 下面来说明一下是如何实现的: 1.安装VMware.Workstation. 2.安装Fedora10. 3.完成 ...
- HDFS 原理、架构与特性介绍
本文主要讲述 HDFS原理-架构.副本机制.HDFS负载均衡.机架感知.健壮性.文件删除恢复机制 1:当前HDFS架构详尽分析 HDFS架构 •NameNode •DataNode •Senc ...
- bzoj-2251 外星联络
题意: 给出一个字符串,求出现次数超过1的子串的出现个数. 字符串长度<=3000: 题解: 题目问的是子串的个数.那么首先我们要找到全部的子串. 而字符串的全部后缀的前缀能够不重不漏的表示全部 ...
- [不好分类]iphone手机激活错误的处理过程
同事一台iphone 6s手机,重启后显示无法激活.(欢迎访问viphhs,欢迎转载.https://www.cnblogs.com/viphhs) 百度后尝试更换了手机卡,重新连接wifi,都不能恢 ...
- ecmall时间的问题
$time1 = date("Y-m-d H:i:s", gmtime()); $time = date("Y-m-d H:i:s", time()); / ...
- Python: lambda, map, reduce, filter
在学习python的过程中,lambda的语法时常会使人感到困惑,lambda是什么,为什么要使用lambda,是不是必须使用lambda? 下面就上面的问题进行一下解答. 1.lambda是什么? ...