0、JSON簡介
JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式。易於人閱讀和編寫。同時也易於機器解析和生成。
JSON采用完全獨立於語言的文本格式,但是也使用了類似於C語言家族的習慣(包括C, C++, C#, Java, JavaScript, Perl, Python等)。
這些特性使JSON成為理想的數據交換語言。 跟XML相比,JSON的優勢在於格式簡潔短小,特別是在處理大量復雜數據的時候,這個優勢便顯得非常突出。
從各瀏覽器的支持來看,JSON解決了因不同瀏覽器對XML DOM解析方式不同而引起的問題。
★JASON的學習資料
http://www.json.org/ (英文) http://www.json.org/json-zh.html (中文)
http://www.w3school.com.cn/json/
http://www.ibm.com/developerworks/cn/web/wa-lo-json/
json標准 https://tools.ietf.org/html/rfc7159
★JASON-C的簡介
JSON庫多種多樣,但是JSON-C由於兼容性好,支持UTF-8,所以使用比較廣泛。
就json來說,由於結構比較簡單,不用庫也是可以的。
但json-c提供了超出json范圍的一些功能,實際上完成了數據序列化和反序列化,數據的存儲和檢索,數據對象化等功能。還是非常有使用價值的。
https://github.com/json-c/json-c/wiki
官方API手冊 : http://json-c.github.io/json-c/json-c-0.13.1/doc/html/index.html
http://zengriguang.blog.163.com/blog/static/17076248720121080187635/
1、下載安裝JSON-C
有以下兩種方式安裝json-c
1.1、使用yum工具安裝
[root]# yum install json-c json-c-devel
1.2、下載源碼編譯安裝
//獲取源代碼
[root]# yum install git
[root]# git clone https://github.com/json-c/json-c.git
//編譯安裝
[root]# cd json-c
[root]# ./autogen.sh
[root]# ./configure
[root]# make
[root]# make install
2、使用JSON-C
如果你的Linux中沒有pkgconfig
//安裝pkgconfig [root]# yum install pkgconfig
編譯使用JSON-C頭文件,鏈接JSON-C庫時需要在makefile中增加如下依賴關系
CFLAGS += $(shell pkg-config --cflags json-c) LDFLAGS += $(shell pkg-config --libs json-c)
//或者簡單一點
gcc `pkg-config --cflags --libs json-c` -o target xxx.c
3、例子
4、JSON-C的README.md文件
`json-c` ======== Building on Unix with `git`, `gcc` and `autotools` -------------------------------------------------- Home page for json-c: https://github.com/json-c/json-c/wiki Caution: do **NOT** use sources from svn.metaparadigm.com, they are old. Prerequisites: - `gcc`, `clang`, or another C compiler - `libtool` If you're not using a release tarball, you'll also need: - `autoconf` (`autoreconf`) - `automake` Make sure you have a complete `libtool` install, including `libtoolize`. `json-c` GitHub repo: https://github.com/json-c/json-c ```bash $ git clone https://github.com/json-c/json-c.git $ cd json-c $ sh autogen.sh ``` followed by ```bash $ ./configure $ make $ make install ``` To build and run the test programs: ```bash $ make check ``` Linking to `libjson-c` ---------------------- If your system has `pkgconfig`, then you can just add this to your `makefile`: ```make CFLAGS += $(shell pkg-config --cflags json-c) LDFLAGS += $(shell pkg-config --libs json-c) ``` Without `pkgconfig`, you would do something like this: ```make JSON_C_DIR=/path/to/json_c/install CFLAGS += -I$(JSON_C_DIR)/include/json-c LDFLAGS+= -L$(JSON_C_DIR)/lib -ljson-c ```