Jansson是用于编码,解码和生成JSON数据的C语言库。它的特性:
- 简单直观的API和数据模型
- 全面的文档
- 不依赖其他库
- 全面支持Unicode(UTF-8)
- 大量测试套件
0.下载jansson https://github.com/akheron/jansson/releases
1.安装jansson
1 2 3 |
|
2.编程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
我主要是使用字符串的形式,参考的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");
return JSON_OBJECT;
}
if (json_is_array(value))
{
printf("json_is_array");
return JSON_ARRAY;
}
if (json_is_string(value))
{
printf("json_is_string");
return JSON_STRING;
}
if (json_is_integer(value))
{
printf("json_is_integer");
return JSON_INTEGER;
}
if (json_is_real(value))
{
printf("json_is_real");
return JSON_REAL;
}
if (json_is_number(value))
{
printf("json_is_number");
}
if (json_is_boolean(value))
{
printf("json_is_boolean");
}
if (json_is_null(value))
{
printf("json_is_null");
return JSON_NULL;
}
if (json_is_true(value))
{
printf("json_boolean(1)");
return JSON_TRUE;
}
if (json_is_false(value))
{
printf("json_boolean(0)");
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", size);
/* 取出object中的值 */
//struct policy iter_get_value(json_t *object)
char *result;
result = json_dumps(object, JSON_PRESERVE_ORDER);
printf("result=%s", result);
/* 判断读取的jansson类型 */
printf("判断是什么类型");
my_json_type(object);
printf("result_size = %d", 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", i,json_string_value(iter_values[i]));
if ((iter = json_object_iter_next(object, iter)) == NULL)
{
//printf("iterate end");
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", 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", policyinfo.keyword[i]);
printf ("value = %s", policyinfo.keycount[i]);
i++;
}
}
#endif
编译:
gcc test.c -ljansson