本帖最后由 雷鹏 于 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 [[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);
- }
- 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:[[1, 2], {"cool": true}]
out:["test"]
out:["foobarbaz"]
其他信息
附源代码
mdkJansson.zip
(804.38 KB, 下载次数: 142)
|