添加库
下载Pack安装包
Jansson安装包下载链接:官网链接
下载之后,双击完成安装。
添加库到工程
-
点击下图图标,进入软件包管理界面
-
勾选Data Exchange->JSON->Jansson选项,然后选择OK。
此时工程中已经添加了jansson库。
-
修改Heap Size
解析和生成json的时候要保证有足够的堆空间,如果堆大小不够会处理失败。一般设置3KB的heap。我这里设置了8K。
-
使用jansson
在添加了jansson库之后,只需要包含 jansson.h 头文件,即可使用其API文件
测试代码如下:
#include <jansson.h>
void jansson_pack_test(void)
{
json_t *root;
char *out;
/* Build an empty JSON object */
root = json_pack("{}");
out = json_dumps(root, JSON_ENCODE_ANY);
PRINTF("out:%s\r\n", out);
json_delete(root);
//free(root);
free(out);
/* Build the JSON object {"foo": 42, "bar": 7} */
root = json_pack("{sisi}", "foo", 42, "bar", 7);
out = json_dumps(root, JSON_ENCODE_ANY);
PRINTF("out:%s\r\n", out);
json_delete(root);
//free(root);
free(out);
/* Like above, ':', ',' and whitespace are ignored */
root = json_pack("{s:i, s:i}", "foo", 42, "bar", 7);
out = json_dumps(root, JSON_ENCODE_ANY);
PRINTF("out:%s\r\n", out);
json_delete(root);
//free(root);
free(out);
/* Build the JSON array [[1, 2], {"cool": true}] */
root = json_pack("[[i,i],{s:b}]", 1, 2, "cool", 1);
out = json_dumps(root, JSON_ENCODE_ANY);
PRINTF("out:%s\r\n", out);
json_delete(root);
//free(root);
free(out);
/* Build a string from a non-null terminated buffer */
char buffer[4] = {'t', 'e', 's', 't'};
root = json_pack("[s#]", buffer, 4);
out = json_dumps(root, JSON_ENCODE_ANY);
PRINTF("out:%s\r\n", out);
json_delete(root);
//free(root);
free(out);
/* Concatenate strings together to build the JSON string "foobarbaz" */
root = json_pack("[s++]", "foo", "bar", "baz");
out = json_dumps(root, JSON_ENCODE_ANY);
PRINTF("out:%s\r\n", out);
json_delete(root);
//free(root);
free(out);
}
void jansson_unpack_test(void)
{
int myint1,myint2,myint3;
const char *str1,*str2;
json_t *root;
char rec[100];
json_error_t *err;
if(RS485_2->rx_success_flag == 1)
{
memcpy(rec,RS485_2->rxbuf,RS485_2->rx_buf_size);
root = json_loads(rec, JSON_DECODE_ANY, err);
json_unpack(root, "{s?s,s?i,s?i}","a", &str1,"b", &myint2,"c", &myint3);
//
RS485_2->rx_success_flag = 0;
}
}
API使用说明
Array
- json_t *json_array(void)
新建JSON数组。 - size_t json_array_size(const json_t *array)
获取json数组的长度(数组中值的个数)。
JSON字符串root:
{
"Model":"DCU",
"Num":1,
"Param":[
{
"Obstacle":[
{
"Dev":"TIM320"
}
]
},
{
"asd":1
}
]
}
...
array = json_object_get(root, "Param");
array_len = json_array_size(array);
返回值 = 2
...
- json_t *json_array_get(const json_t *array, size_t index)
获取指定序号(1~Len-1)的对象,返回数组对象句柄,或者null。
JSON字符串root:
{
"Model":"jjj",
"Num":1,
"Param":[
"aaa",
"bbb",
"ccc"
]
}
...
array = json_object_get(root, "Param");
array_len = json_array_size(array);
array1 = json_array_get(array, 2);
memcpy(&Param.Dev_Model_value[0],json_string_value(array1), json_string_length(array1));
Param.Dev_Model_value的值为:ccc
...
-
int json_array_set(json_t *array, size_t index, json_t *value)
数组设置函数,用value替换array.index。 -
int json_array_set_new(json_t *array, size_t index, json_t *value)
-
int json_array_append(json_t *array, json_t *value)
数组追加值到数组末端,数组长度+1. -
int json_array_append_new(json_t *array, json_t *value)
数组拼接函数,将数组值拼接到数组尾部,数组长度+1。
value = json_string("Music"); //新建数组内容
len = json_array_append(array, value);
- int json_array_insert(json_t *array, size_t index, json_t *value)
- int json_array_insert_new(json_t *array, size_t index, json_t *value)
- int json_array_remove(json_t *array, size_t index)
- int json_array_clear(json_t *array)
- int json_array_extend(json_t *array, json_t *other_array)
- json_array_foreach(array, index, value)
Object
-
json_t *json_object(void)
新建 json_t 格式的对象。 -
size_t json_object_size(const json_t *object)
获取 object 的长度。
JSON字符串root:
{
"Model":"D",
"Num":1,
"Param":[
{
"Obstacle":[
{
"Dev":"TI20"
}
]
}
]
}
...
obj_len = json_object_size(root);
返回值=3
...
- json_t *json_object_get(const json_t *object, const char *key)
获取指定键值对。返回键值对的引用句柄。
JSON字符串root:
{
"Model":"D",
"Num":1,
"Param":[
{
"Obstacle":[
{
"Dev":"TI20"
}
]
}
]
}
...
array = json_object_get(root, "Model");
此时array指向 Model 键值对,可以使用值提取函数获得值。
...
- int json_object_set(json_t *object, const char *key, json_t *value)
- int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
- int json_object_set_new(json_t *object, const char *key, json_t *value)
- int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value)
- int json_object_del(json_t *object, const char *key)
- int json_object_clear(json_t *object)
- int json_object_update(json_t *object, json_t *other)
- int json_object_update_existing(json_t *object, json_t *other)
- int json_object_update_missing(json_t *object, json_t *other)
- int json_object_update_new(json_t *object, json_t *other)
- int json_object_update_existing_new(json_t *object, json_t *other)
- int json_object_update_missing_new(json_t *object, json_t *other)
- int json_object_update_recursive(json_t *object, json_t *other)
- json_object_foreach(object, key, value)
- json_object_foreach_safe(object, tmp, key, value)
- void *json_object_iter(json_t *object)
- void *json_object_iter_at(json_t *object, const char *key)
- void *json_object_iter_next(json_t *object, void *iter)
- const char *json_object_iter_key(void *iter)
- json_t *json_object_iter_value(void *iter)
- int json_object_iter_set(json_t *object, void *iter, json_t *value)
- int json_object_iter_set_new(json_t *object, void *iter, json_t *value)
- void *json_object_key_to_iter(const char *key)
- void json_object_seed(size_t seed)
数据打包
-
json_t *json_pack(const char *fmt, …);
将数据按照json格式打包。 -
json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, …);
-
json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap);
数据编码
- char *json_dumps(const json_t *json, size_t flags);
- int json_dumpf(const json_t *json, FILE *output, size_t flags);
- int json_dump_file(const json_t *json, const char *path, size_t flags);
- int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags);
数据解包
- int json_unpack(json_t *root, const char *fmt, …);
- int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, …);
- int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap);
数据解码
-
json_t *json_loads(const char *input, size_t flags, json_error_t *error);
将JSON字符串从utf-8解码到字符形式。 -
json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error);
-
json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
-
json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
-
json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error);
相关资料
- 官网地址:
http://jansson.readthedocs.io/en/latest/
- Jansson API文档:
http://jansson.readthedocs.io/en/latest/apiref.html