歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux下Json庫的編譯及代碼測試

Linux下Json庫的編譯及代碼測試

日期:2017/3/1 9:59:42   编辑:Linux編程

大部分情況下,我們需要對字符串進行解析,判斷參數開始及結尾才能獲取對我們有用的信息/參數,如果使用Json庫,那對字符串的解析就顯得非常的easy了,而Jsoncpp是用於c++的,讓我們拭目以待吧。

一、Json基礎

JSON(JavascriptObject Notation)是一種輕量級的數據交換語言,以文字為基礎,且易於讓人閱讀。盡管JSON是在Javascript的一個子集,但JSON是獨立於語言的文本格式,並且采用了類似於C語言家族的一些習慣。JSON建構有兩種結構:

1) “名稱/值”對的集合(A collection of name/value pairs)。不同的語言中,它被理解為對象(object),記錄(record),結構(struct),字典(dictionary),哈希表(hash table),有鍵列表(keyed list),或者關聯數組 (associative array)。

2)值的有序列表(An ordered list of values)。在大部分語言中,它被理解為數組(array)。

例如,現創建一個新的 JavaScript 變量,然後將 JSON格式的數據字符串直接賦值給它:

varpeople = {

"programmers":[

{"firstName": "Brett", "lastName":"McLaughlin","email": "aaaa" },

{"firstName": "Jason","lastName":"Hunter", "email": "bbbb" },

{"firstName": "Elliotte","lastName":"Harold", "email": "cccc" }

],

"authors":[

{"firstName": "Isaac", "lastName":"Asimov", "genre": "science fiction" },

{"firstName": "Tad", "lastName":"Williams", "genre": "fantasy" },

{"firstName": "Frank", "lastName":"Peretti", "genre": "christian fiction" }

],

"musicians":[

{"firstName": "Eric", "lastName":"Clapton", "instrument": "guitar" },

{"firstName": "Sergei", "lastName":"Rachmaninoff", "instrument": "piano" }

]}

這非常簡單;現在 people包含前面看到的 JSON 格式的數據。

二、編譯Json庫

jsoncpp是一個c++封裝的json包,跨平台支持windows、linux、unix等多系統。在windows 下面使用比較簡單,直接往vc裡面添加項目就可以了。linux下面編譯需要使用到scons,scons是python工具,需要先安裝好python。

1.下載源碼包並解壓scons-2.2.0.tar.gz

[root@localhost nfs]#tarxzf scons-2.2.0.tar.gz

[root@localhostnfs]#cd scons-2.2.0

[[email protected]]# ls

CHANGES.txt MANIFEST README.txt sconsign.1 setup.cfg

engine os_spawnv_fix.diff RELEASE.txt scons-time.1 setup.py

LICENSE.txt PKG-INFO scons.1 script

2.設置環境變量(為scons-2.2.0的目錄)

[root@localhost jsoncpp-src-0.5.0]#export MYSCONS=/work/nfs/scons-2.2.0

[root@localhost jsoncpp-src-0.5.0]#export SCONS_LIB_DIR=$MYSCONS/engine

3.下載源碼包並解壓jsoncpp-src-0.5.0.tar.gz

[root@localhost nfs]# tar xzfjsoncpp-src-0.5.0.tar.gz

[root@localhostnfs]# cd jsoncpp-src-0.5.0

[[email protected]]#ls

AUTHORS doxybuild.py makefiles scons-tools test

devtools include makerelease.py SConstruct version

doc LICENSE README.txt src

4.編譯

[[email protected]]#python$MYSCONS/script/scons platform=linux-gcc

[[email protected]]# ls include/json/

autolink.h features.h json.h value.h //頭文件

config.h forwards.h reader.h writer.h

[[email protected]]#ls libs/linux-gcc-4.1.2/

libjson_linux-gcc-4.1.2_libmt.a libjson_linux-gcc-4.1.2_libmt.so //庫

Copyright © Linux教程網 All Rights Reserved