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 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);