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


前言
在這篇文章中博主簡單介紹了如何把cJSON移植到STM32上,實際上,keil環境下已經有官方的JSON庫了——Jansson。下面是講解如何導入和使用Jansson。

 

 

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

安裝並導入工程
下載Keil.Jansson.1.0.0.pack后雙擊安裝,打開keil工程,點擊下圖的圖標配置Json庫到工程里。

 

 

接下來按圖點擊,最后點擊OK。

 

 

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

 

 

測試代碼
之后可以使用下面的代碼測試打包json數據。

#include <jansson.h>

//jansson Test
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);
}

打印

out:{}
out:{
"foo": 42,
"bar": 7
}
out:{
"foo": 42,
"bar": 7
}
out:[[1, 2], {"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.21a
注意事項
解析和生成json的時候要保證有足夠的堆空間,如果堆大小不夠會處理失敗。博主一般設置3KB的heap。

 

 

關於中文編碼
在評論區有位朋友問為什么用UTF-8編碼打包的Json打印出來是亂碼。經過博主幾天的查資料,現在問題是這樣的:實際上用Jansson編碼UTF-8的中文是沒有問題的,有問題的是串口的打印問題。

 

 

這是我測試串口打印中文,首先說明幾點,我的文件保存格式是keil默認的格式,也就是ANSI,大家打開記事本然后點擊另存為,在保存路徑下面可以選擇其編碼格式:

 

 

把串口打印出來的數據拷貝到Notepad++,拷貝前先新建一個文檔並以ANSI編碼:

 

 

拷貝過去后沒有出現任何亂碼,可見串口打印顯示的是ANSI編碼字符。
在Notepad++的菜單欄,選擇「格式」,「以UTF-8編碼」,有無BOM都行,然后看到后面的亂碼正常顯示中文了:

 

 

而且,如果使用非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);

下面放上本小節測試代碼。

測試代碼
/* 文件以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);

2018年6月4日更新:

實際上代碼末尾使用free()來釋放內存不夠規范,可能會造成內存泄漏,應該使用json_object_clear()接口清理(待驗證)。具體方法最好查看相關文檔:

json_dumps:http://jansson.readthedocs.io/en/latest/apiref.html#c.json_dumps
json_object_clear:http://jansson.readthedocs.io/en/latest/apiref.html#c.json_object_clear
---------------------
作者:閼男秀
來源:CSDN
原文:https://blog.csdn.net/yannanxiu/article/details/52712723
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM