Linux下Json库的编译及代码测试
一、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
[root@localhostscons-2.2.0]# 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
[root@localhostjsoncpp-src-0.5.0]#ls
AUTHORS doxybuild.py makefiles scons-tools test
devtools include makerelease.py SConstruct version
doc LICENSE README.txt src
4.编译
[root@localhostjsoncpp-src-0.5.0]#python$MYSCONS/script/scons platform=linux-gcc
[root@localhostjsoncpp-src-0.5.0]# ls include/json/
autolink.h features.h json.h value.h //头文件
config.h forwards.h reader.h writer.h
[root@localhostjsoncpp-src-0.5.0]#ls libs/linux-gcc-4.1.2/
libjson_linux-gcc-4.1.2_libmt.a libjson_linux-gcc-4.1.2_libmt.so //库
三、测试
1.编写json_test.cpp
#include<iostream>
#include<string>
#include"./json/json.h"
using namespace std;
int main(void)
{
stringstrJson ="{\"key1\":\"value1\",\"array\":[{\"key2\":\"value2\",\"key3\":\"aa\"},{\"key2\":\"value3\",\"key3\":\"bb\"},{\"key2\":\"value4\",\"key3\":\"cc\"}]}";
Json::Readerreader;
Json::StyledWriterstyled_writer;
Json::Valueval;
if(!reader.parse(strJson,val))
return-1;
stringstr = val["key1"].asString();
cout<< "key1:" << str << endl;
Json::Valueobj_array = val["array"];
cout<< styled_writer.write(obj_array) << endl;
for(int i = 0; i < obj_array.size(); i++)
{
str= obj_array[i]["key2"].asString();
cout<< "key2[" << i << "]:" << str<< endl;
str= obj_array[i]["key3"].asString();
cout<< "key2[" << i << "]:" << str<< endl;
}
return 0;
}
2.创建符号链接
[root@localhost jsoncpp-src-0.5.0]#ln -s/work/nfs/jsoncpp-src-0.5.0/libs/linux-gcc-4.1.2/libjson_linux-gcc-4.1.2_libmt.so /usr/local/lib/libjson.so
[root@localhost jsoncpp-src-0.5.0]#/sbin/ldconfig //更新库
3.编译
[root@localhost jsoncpp-src-0.5.0]#g++ json_test.cpp -ljson
4.运行程序
[root@localhostjsoncpp-src-0.5.0]#./a.out
key1:value1
[
{
"key2" : "value2",
"key3" : "aa"
},
{
"key2" : "value3",
"key3" : "bb"
},
{
"key2" : "value4",
"key3" : "cc"
}
]
key2[0]:value2
key2[0]:aa
key2[1]:value3
key2[1]:bb
key2[2]:value4
key2[2]:cc