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数据不 ...
随机推荐
- POI-----POI操作Excel-4、字体
- Java、C++、Python、Ruby、PHP、C#和JavaScript的理解
Java.C++.Python.Ruby.PHP.C#和JavaScript和日本动漫里的那些大家熟悉的动漫人物结合起来.依据他们的身世.个人经历来生动的表达各编程语言的发展历程.原文内容例如以下: ...
- Spring Boot实现STOMP协议的WebSocket
关注公众号:锅外的大佬 每日推送国外优秀的技术翻译文章,励志帮助国内的开发者更好地成长! WebSocket协议是应用程序处理实时消息的方法之一.最常见的替代方案是长轮询(long polling)和 ...
- 将Ubuntu主文件夹里的中文文件夹名称改成英文
方法一: 首先修改现有主文件夹下各文件夹名称: Desktop. Documents. Download. Music. Pictures. Public. Templates. Videos …… ...
- centos6.4中文输入法安装和切换(转载)
1.用root登录,或者切换到root账户(su root): 2.yum install "@Chinese Support"; 3.exit: 4.System→prefere ...
- 【BZOJ3007】拯救小云公主 二分+几何+对偶图
[BZOJ3007]拯救小云公主 Description 英雄又即将踏上拯救公主的道路…… 这次的拯救目标是——爱和正义的小云公主. 英雄来到boss的洞穴门口,他一下子就懵了 ...
- Java类加载器( 死磕7)
[正文]Java类加载器( CLassLoader )死磕7: 基于加密的自定义网络加载器 本小节目录 7.1. 加密传输Server端的源码 7.2. 加密传输Client端的源码 7.3. 使 ...
- Redis(二)延迟队列
1.目录 延迟队列 进一步优化 2.延迟队列 package com.redis; import java.lang.reflect.Type; import java.util.Set; impor ...
- mybatis入门(八)
mybatis入门---更新和删除 <!-- 删除用户 --> <delete id="deleteUser" parameterType="java. ...
- 软件版本GA,RC,alpha,beta含义
软件版本GA,RC,alpha,beta含义 (1)RC:(Release Candidate) Candidate是候选人的意思,用在软件上就是候选版本.Release.Candidate.就是发行 ...