STM32 Jansson解析json


1. 選擇哪一款開源庫

開源庫有很多,也有相關博文介紹和對比,最后依然選擇了jansson,只因https://code.google.com/p/libjson/source/checkout 編寫該libjson庫的原作者聲明:This project is no longer being actively developed. I have instead started using libjansson.

2. 編譯

下載代碼:

git clone https://github.com/akheron/jansson

 

官方文檔已經相當明確的說明了編譯的過程:https://jansson.readthedocs.org/en/2.5/gettingstarted.html

需要注意一點,在windows上生成vs2008工程時,示例代碼無法編譯通過,在jansson_private_config.h頭文件中增加:

#ifndef HAVE_UINT16_T # define uint16_t unsigned short #endif  #ifndef HAVE_UINT8_T # define uint8_t unsigned char #endif

 

即可正常使用jansson

3. 示例代碼

這里指闡述win下生成project后如何使用,linux下面后期再補上;

#include "jansson.h" #include <assert.h> int _tmain(int argc, _TCHAR* argv[]) { const char *pJson = "[ \ { \ \"object1-key1\": 123, \ \"object1-key2\": \"abc\" \ }, \ { \ \"test\": \"x\", \ \"test2\": \"x2\" \ }, \ { \ \"key\": \"y\" \ } \ ]" ; json_error_t error; json_t *pRoot = json_loads(pJson, JSON_REJECT_DUPLICATES, &error); if ( pRoot == NULL ) { printf("%s", error.text); return -1; } int value1 = 0; const char *value2 = NULL; const char *value3 = NULL; const char *value4 = NULL; const char *value5 = NULL; ///first solution to analyze quickly when u know keys. int iRet = json_unpack(pRoot, "[{s:i,s:s},{s:s,s:s},{s:s}]", "object1-key1", &value1,  "object1-key2", &value2, "test", &value3, "test2",&value4, "key", &value5); assert( iRet == 0 ); json_decref(pRoot); return 0; }

 

應jansson提供了兩個json_load*, json_unpack*, json_pack*這幾個強大的接口,使得構建和解析json格式變得異常簡單了~

4. 文檔比較全面:https://jansson.readthedocs.org/en/2.5/apiref.html

通過閱讀該文檔,就幾乎沒有問題了,而且源碼可讀性非常高,易於擴展和維護;

Examples:

/* root is the JSON integer 42 */
int myint; json_unpack(root, "i", &myint); assert(myint == 42); /* root is the JSON object {"foo": "bar", "quux": true} */ const char *str; int boolean; json_unpack(root, "{s:s, s:b}", "foo", &str, "quux", &boolean); assert(strcmp(str, "bar") == 0 && boolean == 1); /* root is the JSON array [[1, 2], {"baz": null} */ json_error_t error; json_unpack_ex(root, &error, JSON_VALIDATE_ONLY, "[[i,i], {s:n}]", "baz"); /* returns 0 for validation success, nothing is extracted */ /* root is the JSON array [1, 2, 3, 4, 5] */ int myint1, myint2; json_unpack(root, "[ii!]", &myint1, &myint2); /* returns -1 for failed validation */ /* root is an empty JSON object */ int myint = 0, myint2 = 0, myint3 = 0; json_unpack(root, "{s?i, s?[ii]}", "foo", &myint1, "bar", &myint2, &myint3); /* myint1, myint2 or myint3 is no touched as "foo" and "bar" don't exist */



/* Build an empty JSON object */
json_pack("{}"); /* Build the JSON object {"foo": 42, "bar": 7} */ json_pack("{sisi}", "foo", 42, "bar", 7); /* Like above, ':', ',' and whitespace are ignored */ json_pack("{s:i, s:i}", "foo", 42, "bar", 7); /* Build the JSON array [[1, 2], {"cool": true}] */ json_pack("[[i,i],{s:b}]", 1, 2, "cool", 1); /* Build a string from a non-null terminated buffer */ char buffer[4] = {'t', 'e', 's', 't'}; json_pack("s#", buffer, 4); /* Concatenate strings together to build the JSON string "foobarbaz" */ json_pack("s++", "foo", "bar", "baz"); /* Create an empty object or array when optional members are missing */ json_pack("{s:s*,s:o*,s:O*}", "foo", NULL, "bar", NULL, "baz", NULL); json_pack("[s*,o*,O*]", NULL, NULL, NULL);


免責聲明!

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



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