使用keil提供的JSON库——Jansson 在V5上移植
本帖最后由 雷鹏 于 2018-9-3 22:53 编辑简单介绍了如何把cJSON移植到STM32上,实际上,keil环境下已经有官方的JSON库了——Jansson。下面是讲解如何导入和使用Jansson移植到V5开发板上。
下载地址:http://www2.keil.com/mdk5/partnerpacks/
测试代码
[*]
[*]#include "bsp.h"
[*]#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);
[*] 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 [, {"cool": true}] */
[*] root = json_pack("[,{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 = {'t', 'e', 's', 't'};
[*] root = json_pack("", 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("", "foo", "bar", "baz");
[*]
[*] out = json_dumps(root, JSON_ENCODE_ANY);
[*] printf("out:%s\r\n", out);
[*] free(root);
[*] free(out);
[*]}
[*]
[*]int main(void)
[*]{
[*] bsp_Init();
[*] printf("APP RUN..\r\n");
[*] jansson_pack_test();
[*] while(1)
[*] {}
[*]
[*]}
打印APP RUN..
out:{}
out:{"bar": 7, "foo": 42}
out:{"bar": 7, "foo": 42}
out:[, {"cool": true}]
out:["test"]
out:["foobarbaz"]
其他信息
[*]Jansson官网:http://jansson.readthedocs.io/en/latest/
[*]Jansson API文档:http://jansson.readthedocs.io/en/latest/apiref.html
[*]keil版本:uVision 5.14a
[*]
static/image/hrline/2.gif
附源代码
static/image/hrline/4.gif
非常感谢楼主分享{:8:} 这个test代码有问题,跑3遍就出错 ^张浩然 发表于 2018-12-21 10:27
这个test代码有问题,跑3遍就出错
用cJSON吧 eric2013 发表于 2018-12-22 01:50
用cJSON吧
最近也在看这个,和java对cjson格式,以前都是包头加指令就好了 我们用过JSMN,这个功能比较少,编译出来的Code Size很小。
如果对提及敏感,可以试试这个。
https://zserge.com/jsmn.html
不过,不知道跟Jansson比哪个更小。
Code (inc. data) RO Data RW Data ZI Data Debug Object Name
810 0 0 0 0 5869 jsmn.o
----- 发表于 2018-12-24 09:26
我们用过JSMN,这个功能比较少,编译出来的Code Size很小。
如果对提及敏感,可以试试这个。
https://zse ...
这个看起来不错。。。
页:
[1]