【STM32】使用keil提供的JSON库——Jansson【转】

来源:互联网 发布:万方数据库和中国知网 编辑:程序博客网 时间:2024/06/16 09:57

来自:http://blog.csdn.net/yannanxiu/article/details/52712723

前言

在这篇文章中博主简单介绍了如何把cJSON移植到STM32上,实际上,keil环境下已经有官方的JSON库了——Jansson。下面是讲解如何导入和使用Jansson。

keil::Jansson

下载地址:http://www2.keil.com/mdk5/partnerpacks/

安装并导入工程

下载Keil.Jansson.1.0.0.pack后双击安装,打开keil工程,点击下图的图标配置Json库到工程里。

设置keil环境

接下来按图点击,最后点击OK。

这里写图片描述

最后工程目录下面就有会Jansson的lib文件了。

导入成功

测试代码

之后可以使用下面的代码测试打包json数据。

#include <jansson.h>//jansson Testvoid 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);    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);    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);    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);    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);    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);    free(root);    free(out);}
  • 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

打印

out:{}out:{ "foo": 42, "bar": 7}out:{ "foo": 42, "bar": 7}out:[[1, 2], {"cool": true}]out:["test"]out:["foobarbaz"]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

其他信息

  1. Jansson官网:http://jansson.readthedocs.io/en/latest/
  2. Jansson API文档:http://jansson.readthedocs.io/en/latest/apiref.html
  3. keil版本:uVision 5.21a

注意事项

  • 解析和生成json的时候要保证有足够的堆空间,如果堆大小不够会处理失败。博主一般设置3KB的heap。 
    修改堆大小

关于中文编码

在评论区有位朋友问为什么用UTF-8编码打包的Json打印出来是乱码。经过博主几天的查资料,现在问题是这样的:实际上用Jansson编码UTF-8的中文是没有问题的,有问题的是串口的打印问题。

串口打印

这是我测试串口打印中文,首先说明几点,我的文件保存格式是keil默认的格式,也就是ANSI,大家打开记事本然后点击另存为,在保存路径下面可以选择其编码格式:

文本文档选择编码格式

把串口打印出来的数据拷贝到Notepad++,拷贝前先新建一个文档并以ANSI编码:

Noted++ ANSI

拷贝过去后没有出现任何乱码,可见串口打印显示的是ANSI编码字符。 
在Notepad++的菜单栏,选择「格式」,「以UTF-8编码」,有无BOM都行,然后看到后面的乱码正常显示中文了:

Noted++ UTF-8

而且,如果使用非UTF-8字符打包Json,Jansson是会返回「Invalid UTF-8 string」错误信息的,当然前提是使用json_pack_ex函数。

/* 文件以ANSI格式存储,keil MDK默认的格式 */char *zhongwen = "中文";root = json_pack_ex(&e, JSON_ENCODE_ANY, "[s]", zhongwen);printf("e:%s\r\n", e.text);
  • 1
  • 2
  • 3
  • 4

下面放上本小节测试代码。

测试代码

/* 文件以ANSI格式存储,keil MDK默认的格式 */char *zhongwen = "中文";char *zhongwen_utf8 = "\xE4\xB8\xAD\xE6\x96\x87";printf("zhongwen:%s\r\n", zhongwen);printf("zhongwen_utf8:%s\r\n", zhongwen_utf8);json_error_t e;root = json_pack_ex(&e, JSON_ENCODE_ANY, "[s]", zhongwen);printf("e:%s\r\n", e.text);root = json_pack_ex(&e, JSON_ENCODE_ANY, "[s]", zhongwen_utf8);printf("e:%s\r\n", e.text);out = json_dumps(root, JSON_ENCODE_ANY);printf("out:%s\r\n", out);free(root);free(out);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

本文最后编辑时间:2016年12月


原创粉丝点击