嵌入式json:jansson应用(2)

如果止步于此,就不是jansson了,下一章节,给大家介绍json_pack和json_unpack两个神兵利器

书接上回,接下来,说一下 jansson这个适用于嵌入式json库的2个神器函数json_pack和json_unpack。

json_t *json_pack(const char *fmt, ...)

int json_unpack(json_t *root, const char *fmt, ...)

fmt的格式如下:

s (string) [const char *]
Convert a null terminated UTF-8 string to a JSON string.
s? (string) [const char *]
Like s, but if the argument is NULL, output a JSON null value.

New in version 2.8.

s* (string) [const char *]
Like s, but if the argument is NULL, do not output any value. This format can only be used inside an object or an array. If used inside an object, the corresponding key is additionally suppressed when the value is omitted. See below for an example.

New in version 2.11.

s# (string) [const char *, int]
Convert a UTF-8 buffer of a given length to a JSON string.

New in version 2.5.

s% (string) [const char *, size_t]
Like s# but the length argument is of type size_t.

New in version 2.6.

+ [const char *]
Like s, but concatenate to the previous string. Only valid after s, s#, + or +#.

New in version 2.5.

+# [const char *, int]
Like s#, but concatenate to the previous string. Only valid after s, s#, + or +#.

New in version 2.5.

+% (string) [const char *, size_t]
Like +# but the length argument is of type size_t.

New in version 2.6.

n (null)
Output a JSON null value. No argument is consumed.
b (boolean) [int]
Convert a C int to JSON boolean value. Zero is converted to false and non-zero to true.
i (integer) [int]
Convert a C int to JSON integer.
I (integer) [json_int_t]
Convert a C json_int_t to JSON integer.
f (real) [double]
Convert a C double to JSON real.
o (any value) [json_t *]
Output any given JSON value as-is. If the value is added to an array or object, the reference to the value passed to o is stolen by the container.
O (any value) [json_t *]
Like o, but the argument’s reference count is incremented. This is useful if you pack into an array or object and want to keep the reference for the JSON value consumed by O to yourself.
o?, O? (any value) [json_t *]
Like o and O, respectively, but if the argument is NULL, output a JSON null value.

New in version 2.8.

o*, O* (any value) [json_t *]
Like o and O, respectively, but if the argument is NULL, do not output any value. This format can only be used inside an object or an array. If used inside an object, the corresponding key is additionally suppressed. See below for an example.

New in version 2.11.

[fmt] (array)
Build an array with contents from the inner format string. fmt may contain objects and arrays, i.e. recursive value building is supported.
{fmt} (object)
Build an object with contents from the inner format string fmt. The first, third, etc. format specifier represent a key, and must be a string (see s, s#, + and +# above), as object keys are always strings. The second, fourth, etc. format specifier represent a value. Any value may be an object or array, i.e. recursive value building is supported.

在大部分工作中,只需要构建出一个简单的json字符串/解析出字符串即可,所以这2个函数可以满足大部分场景了,相比于前一篇文章提到的那些函数,明显json_pack和json_unpack更加简洁高效

可能看到这边,大部分宝宝一脸懵逼,这边给一个json_pack的例子,将数据打包成json字符串,里面包含了数组,对象,和普通键值对。

例子:

void myjsonpack(){
	int id=30;
	//char buf[10]={128,128,0,24,25};
	//char buf[10]={'t', 'e', 's', 't'};
	json_t* root=json_pack("{s:i,s:s,s:i,s:i,s:{s:i,s:s},s:[i,i,i,i]}", "id", 1, "heartbeat", "on","num",128,"price",24,"status","zhekou",80,"zhangsan","12587","shangpin",128,128,0,24);
	char* str = NULL;
	str = json_dumps(root, JSON_PRESERVE_ORDER);
//s_repon = json_dumps(root, JSON_COMPACT);
	printf("str = %s \n",str);
	free(str);
	json_decref(root);
}

输出打印:

str={
    "id": 1,
    "heartbeat": "on",
    "num": 128,
    "price": 24,
    "status": {
        "zhekou": 80,
        "zhangsan": "12587"
    },
    "shangpin": [
        128,
        128,
        0,
        24
    ]
}

 看了例子大部分代码,很多宝宝其实就能明白,其实和printf有异曲同工之妙。这可能才是代码原本应该有的样子吧

接下里,我们再来看看json_unpack函数

int json_unpack(json_t *root, const char *fmt, ...)

s (string) [const char *]
Convert a JSON string to a pointer to a null terminated UTF-8 string. The resulting string is extracted by using json_string_value() internally, so it exists as long as there are still references to the corresponding JSON string.
s% (string) [const char *, size_t *]
Convert a JSON string to a pointer to a null terminated UTF-8 string and its length.

New in version 2.6.

n (null)
Expect a JSON null value. Nothing is extracted.
b (boolean) [int]
Convert a JSON boolean value to a C int, so that true is converted to 1 and false to 0.
i (integer) [int]
Convert a JSON integer to C int.
I (integer) [json_int_t]
Convert a JSON integer to C json_int_t.
f (real) [double]
Convert a JSON real to C double.
F (integer or real) [double]
Convert a JSON number (integer or real) to C double.
o (any value) [json_t *]
Store a JSON value with no conversion to a json_t pointer.
O (any value) [json_t *]
Like o, but the JSON value’s reference count is incremented. Storage pointers should be initialized NULL before using unpack. The caller is responsible for releasing all references incremented by unpack, even when an error occurs.
[fmt] (array)
Convert each item in the JSON array according to the inner format string. fmt may contain objects and arrays, i.e. recursive value extraction is supported.
{fmt} (object)
Convert each item in the JSON object according to the inner format string fmt. The first, third, etc. format specifier represent a key, and must be s. The corresponding argument to unpack functions is read as the object key. The second, fourth, etc. format specifier represent a value and is written to the address given as the corresponding argument. Note that every other argument is read from and every other is written to.

fmt may contain objects and arrays as values, i.e. recursive value extraction is supported.

New in version 2.3: Any s representing a key may be suffixed with a ? to make the key optional. If the key is not found, nothing is extracted. See below for an example.

!
This special format specifier is used to enable the check that all object and array items are accessed, on a per-value basis. It must appear inside an array or object as the last format specifier before the closing bracket or brace. To enable the check globally, use the JSON_STRICT unpacking flag.
*
This special format specifier is the opposite of !. If the JSON_STRICT flag is used, * can be used to disable the strict check on a per-value basis. It must appear inside an array or object as the last format specifier before the closing bracket or brace.

这边给个例子:

json就是上面输出的字符串,我们来解析它

void myjsonunpack(){
	 char *strbuf="{\"id\": 1, \"heartbeat\": \"on\", \"num\": 128, \"price\": 24, 
                 \"status\": {\"zhekou\": 80, \"zhangsan\": \"12587\"}, \"shangpin\": [128, 128, 0, 24]}"; 
  
	 json_error_t err;
	 json_t* root = json_loads(strbuf, 0, &err);
	
	
	 int id,num,price,zhekou;
	 char *headrtbuf,*zhangsanbuf;
	 int buf[10];
     json_unpack(root, "{s:i,s:s,s:i,s:i,s:{s:i,s:s},s:[i,i,i,i]}","id", &id, "heartbeat", 
&headrtbuf,"num",&num,"price",&price,"status","zhekou",&zhekou,"zhangsan",&zhangsanbuf,"shangpin",&buf[0],&buf[1],&buf[2],&buf[3]);

	printf("id:%d,num:%d,price:%d,zhekou:%d\r\n",id,num,price,zhekou);
	printf("heartbuf:%s\r\n",headrtbuf);
	printf("zhangsanbuf:%s\r\n",zhangsanbuf);
	for(int i=0;i<4;i++){
		printf("%d:%d\t",i,buf[i]);
	}

	printf("\r\n");
	free(headrtbuf);
	free(zhangsanbuf);
}

打印输出:

id:1,num:128,price:24,zhekou:80
heartbuf:on
zhangsanbuf:12587
0:128	1:128	2:0	3:24	

对比前一篇文章,就会发现这部分解析json的代码,核心就一个函数json_unpack,利用fmt就可以把我们需要的值解析出来。

关于 jansson 库的内容到此结束。

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值