1.安装jansson

  ./configure
make
make install

2.生成帮助文档

cd doc
make html

编译安装doc时提示 spinx-build not a command

执行下面语句安装sphinx

easy_install -U Sphinx

生成_build文件夹

cd _build/html/

使用火狐浏览器启动帮助文档

 Firefox index.html

3.编程

包含头文件: #include <jansson.h>

编译连接时加库 -ljansson

           gcc –o source source.c –ljansson

如果执行程序出现:error while loading shared libraries: libjansson.so.4: cannot open shared object file: No such file or directory

          找不到libjansson.so.4这个库时用下面方法解决

       //查找libjansson.so.4所在位置

          Whereis libjansson

显示:libjansson: /usr/local/lib/libjansson.a /usr/local/lib/libjansson.la /usr/local/lib/libjansson.so

自己的libjansson.so所在位置为:/usr/local/lib/libjansson.so

       //确定libjansson.so是否真的存在

          cd /usr/local/lib

       //如果存在,执行下面语句,创建一个libjansson.so的符号链接到/usr/lib目录下

          ln -s /usr/local/lib/libjansson.so /usr/lib/libjansson.so.4

       //重新加载库

          ldconfig

 常用接口函数:

json_t *json_string(const char *value)

        返回一个json string的数据类型,转换成这个库可以识别的格式。错误返回NULL,必须是UTF-8格式的。

       Return value: New reference.

       Returns a new JSON string, or NULL on error. value must be a valid UTF-8 encoded Unicode string.

json_t *json_string_nocheck(const char *value)

        与json_string()相同,不检查value的有效性

      Return value: New reference.

      Like json_string(), but doesn’t check that value is valid UTF-8. Use this function only if you are certain that this really is the case (e.g. you have already checked it by other means).

const char *json_string_value(const json_t *string)

       返回json string中的字符串,是c语言的字符串。

      Returns the associated value of string as a null terminated UTF-8 encoded string,

or NULL if string is not a JSON string.

      The retuned value is read-only and must not be modified or freed by the user. It is valid as long as string exists, i.e. as long as its reference count has not dropped to zero.

int json_string_set(const json_t *string, const char *value)

       设置string对应的值,如果value是无效的UTF-8值,设置失败。

     Sets the associated value of string to value. value must be a valid UTF-8 encoded Unicode string. Returns 0 on success and -1 on error.

int json_string_set_nocheck(const json_t *string, const char *value)

       与json_string_set()相同,只是不检查value是否是有效的UTF-8类型

Like json_string_set(), but doesn’t check that value is valid UTF-8. Use this function only if you are certain that this really is the case (e.g. you have already checked it by other means).

  

我主要是使用字符串的形式,参考的jansson-2.6/test/suites/api/test_object.c代码

下面的程序功能是:输出一个json格式的字符串,gcc 的时候别忘了加 -ljansson

需要特别注意的地方时jansson数组的处理,在需要循环的加入数组的时候需要使用 json_deep_copy()函数。如下例子:

json_t copy;

json_t object;

json_t array;

json_object_set_new (object, "test", json_string("testvalue1"));

copy = json_deep_copy(object);
json_array_append(arr, copy); json_object_set_new (object, "test", json_string("testvalue2")); copy = json_deep_copy(object);
json_array_append(arr, copy);

//下面的是处理字符串

#include <stdio.h>
#include <jansson.h> int event_jansson()
{
json_t *objectmsg;
char *result; objectmsg = json_object(); json_object_set_new (objectmsg, "inc", json_string("value-incID"));
json_object_set_new (objectmsg, "src", json_string("a"));
json_object_set_new (objectmsg, "des", json_string("b"));
json_object_set_new (objectmsg, "protocol", json_string("c"));
json_object_set_new (objectmsg, "policy", json_string("d"));
json_object_set_new (objectmsg, "snapshot", json_string("e"));
json_object_set_new (objectmsg, "name", json_string("f"));
json_object_set_new (objectmsg, "Type", json_string("g"));
json_object_set_new (objectmsg, "Path", json_string("h"));
json_object_set_new (objectmsg, "domain", json_string("i")); result = json_dumps(objectmsg, JSON_PRESERVE_ORDER); printf("result=%s\n",result); free(result);
json_decref(objectmsg);  return 0;
} int main()
{
  event_jansson();
return 0;
}

  

下面的程序是从一个json文件中取出内容。

tmp.json文件中的内容是:

{"name1": "value1", "name2": "value2", "name3": "value3", "name4": "value4"}

 vim test.c

#include <string.h>
#include <stdio.h>
#include <jansson.h> /* 关键字个数 */
#define COUNTMAX 256
/* 关键字长度 */
#define LENMAX 256 struct policymsg
{
int size;
char keyword[COUNTMAX][LENMAX];
char keycount[COUNTMAX][LENMAX];
}; /* 判断janson的类型 */
int my_json_type(json_t *value)
{
if(json_is_object(value))
{
printf("json_is_object\n");
return JSON_OBJECT;
} if(json_is_array(value))
{
printf("json_is_array\n");
return JSON_ARRAY;
} if(json_is_string(value))
{
printf("json_is_string\n");
return JSON_STRING;
} if(json_is_integer(value))
{
printf("json_is_integer\n");
return JSON_INTEGER;
} if(json_is_real(value))
{
printf("json_is_real\n");
return JSON_REAL;
} if(json_is_number(value))
{
printf("json_is_number\n");
} if(json_is_boolean(value))
{
printf("json_is_boolean\n");
} if(json_is_null(value))
{
printf("json_is_null\n");
return JSON_NULL;
} if(json_is_true(value))
{
printf("json_boolean(1)\n");
return JSON_TRUE;
} if(json_is_false(value))
{
printf("json_boolean(0)\n");
return JSON_FALSE;
}
} struct policymsg get_policy_json(char *jsonfile)
{
struct policymsg policyinfo;
int i, size;
void *iter;
json_t *object;
json_t *iter_values;
json_error_t error; object = json_object(); object = json_load_file (jsonfile, 0, &error);
policyinfo.size = json_object_size (object);
#if 0
//size = json_object_size (object);
//printf("size=%d\n", size);
/* 取出object中的值 */
//struct policy iter_get_value(json_t *object)
char *result;
result = json_dumps(object, JSON_PRESERVE_ORDER);
printf("result=%s\n", result);
/* 判断读取的jansson类型 */
printf("判断是什么类型\n");
my_json_type(object);
printf("result_size = %d\n", strlen(result));
#endif iter = json_object_iter(object);
i = 0;
while(1)
{
strcpy (policyinfo.keyword[i], json_object_iter_key(iter));
iter_values = json_object_iter_value(iter);
strcpy (policyinfo.keycount[i],json_string_value(iter_values)); //printf("values[%d]=%s\n", i,json_string_value(iter_values[i])); if((iter = json_object_iter_next(object, iter)) == NULL)
{
//printf("iterate end\n");
break;
}
i++;
} #if 0
iter = json_object_iter_at(object, "b");
if(iter)
{
iter_keys[i] = json_object_iter_key(iter);
iter_values[i] = json_object_iter_value(iter);
printf("values[%d]=%s\n", i,json_string_value(iter_values[i]));
}
#endif json_decref(object);
return policyinfo;
} #if 1
int main()
{ //result = "{\"objectmsg\": \"{\"name1\": \"value1\", \"name2\": \"value2\", \"name3\": \"value3\", \"name4\": \"value4\"}\", \"name5\": \"value6\"}";
//{"name1": "value1", "name2": "value2", "name3": "value3", "name4": "value4"}
char *str = "./tmp.json";
struct policymsg policyinfo;
policyinfo = get_policy_json(str);
int i = 0; while ( i < policyinfo.size)
{
printf ("keyword = %s\n", policyinfo.keyword[i]);
printf ("value = %s\n", policyinfo.keycount[i]);
i++;
}
}
#endif

  

编译:

gcc test.c -ljansson

linux 下jansson安装和使用的更多相关文章

  1. Linux下yum安装MySQL

    写这篇文章的原因是:在刚开始使用Linux操作系统时想要搭建LAMP环境,于是开始在Google和百度上各种寻找资料,碰到了不是很多的问题后,我决定写这篇文章总结一下在Linux下yum安装MySQL ...

  2. LINUX下编译安装PHP各种报错大集合

    本文为大家整理汇总了一些linux下编译安装php各种报错大集合 ,感兴趣的同学参考下. nginx1.6.2-mysql5.5.32二进制,php安装报错解决: 123456 [root@clien ...

  3. 【夯实PHP基础系列】linux下yum安装PHP APC

    Alternative PHP Cache(可选PHP缓存),依赖于 PECL扩展库 用源码方式安装,直接yum就行了:首先要安装apc的依赖包:yum install php-pear php-de ...

  4. Linux学习心得之 Linux下ant安装与使用

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Linux学习心得之 Linux下ant安装与使用 1. 前言2. ant安装3. 简单的a ...

  5. Linux下php安装Redis扩展

    说明: 操作系统:CentOS php安装目录:/usr/local/php php.ini配置文件路径:/usr/local/php7/etc/php.ini Nginx安装目录:/usr/loca ...

  6. linux下编译安装vim7.4并安装clang_complete插件

    linux下编译安装vim7.4并安装clang_complete插件 因为debian里软件仓库中下载安装的vim是不支持python写的插件的(可以打开vim,在命令模式先输入:py测试一下),导 ...

  7. linux下编译安装curl

    linux下编译安装curl 1.下载curl git clone https://github.com/curl/curl.git 2.在curl目录下生成configure文件 ./buldcon ...

  8. linux下编译安装boost库

    linux下编译安装boost库 linux下编译安装boost库 1.下载并解压boost 1.58 源代码 下载 解压 2.运行bootstrap.sh 3.使用b2进行构建 构建成功的提示 4. ...

  9. linux下VMware安装出现的问题解决

    linux下VMware安装出现的问题解决 linux下VMware安装出现的问题解决 报错信息 问题1liboverlay-scrollbar.so和libunity-gtk-module.so加载 ...

随机推荐

  1. Python文件编码不可以使用UTF16

    1. The complete Python source file should use a single encoding. Embedding of differently encoded da ...

  2. Linux入门基础(二)——cp、nano命令

  3. Java for LeetCode 090 Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  4. iOSapp内跳转到设置界面

    从app内跳转到设置界面的代码如下: NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIAp ...

  5. mySql执行效率分析

    1.关于SQL查询效率,100w数据,查询只要1秒,与您分享: 机器情况p4: 2.4内存: 1 Gos: windows 2003数据库: ms sql server 2000目的: 查询性能测试, ...

  6. Matlab图像处理(03)-基本概念

    概念定义 动态范围:灰度跨跃的值域称为动态范围.上限取决于饱和度,下限取决于噪声. 对比度:一幅图像中最高和最低灰度级间的灰度差. 空间分辨率:图像中可辨别的最小细节的度量.常用度量每单位距离线对数和 ...

  7. wampserver发布详解

    以下为wampserver发布php并绑定域名的操作 1 在“httpd.conf”文件中查找:Include conf/extra/httpd-vhosts.conf,去掉前面的注释# 2 打开ap ...

  8. image auto fix the View

    image: { flex: 1, width: null, height: null, resizeMode: 'contain' }

  9. 8--json交互

    8.1 为什么要进行json数据交互 json数据格式在接口调用.html页面中较常用,json格式较简单,解析较方便. 比如:webservice接口,传输json数据. 8.2      spri ...

  10. PYTHON 爬虫笔记六:PyQuery库基础用法

    知识点一:PyQuery库详解及其基本使用 初始化 字符串初始化 html = ''' <div> <ul> <li class="item-0"&g ...