两种安装方式:
- 通过cmake安装
- 通过scons安装
cmake安装见cmake安装jsoncpp,scons安装见下文。
1. 安装scons
Download site: https://sourceforge.net/projects/scons/files/scons/2.5.1/
tar zxvf scons-2.5.0.tar.gz
export MYSCONS=/root/file/scons-2.5.0
export SCONS_LIB_DIR=$MYSCONS/engine
MYSCONS要换成你自己文件的路径。
(我还把这两行export添加到了 /etc/profile 文件中了。)
2. 编译安装jsoncpp
tar zxvf jsoncpp-1.8.0.tar.gz
cd jsoncpp-1.8.0
python $MYSCONS/script/scons platform=linux-gcc
3. 路径设置
把源码中的json文件夹拷贝到/usr/local/include目录下:sudo cp -r include/json /usr/local/include/
把生成的.a和.so文件拷贝到/usr/local/lib目录下。
cp libs/linux-gcc-4.8.5/* /usr/local/lib
打开/etc/profile,添加以下内容:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/lib
保存,再来一句: source /etc/profile,修改立即生效。
安装结束。
----------------------------
一个Demo:
#include <iostream>
#include "json/json.h"
using namespace std;
int main()
{
Json::Value root;
root["key1"] = "value1";
root["key2"] = "value2";
Json::Value value31;
value31["key31"] = "value31";
value31["key32"] = "value32";
Json::Value value3;
value3.append(value31); // value3[0] = value31;
root["key3"] = value3;
//for (Json::Value::const_iterator it = value3.begin(); it != value3.end(); it++) {
// Json::Value currentValue = *it;
// for (Json::Value::const_iterator it = currentValue.begin(); it != currentValue.end(); it++) {
// cout << (*it).asString() << endl;
// }
//}
Json::Value value4;
value4["key41"] = "value41";
value4["key42"] = "value42";
root["key4"] = value4;
//for (Json::Value::const_iterator it = value4.begin(); it != value4.end(); it++) {
// cout << (*it).asString() << endl;
//}
Json::Value value5;
value5[0] = "value51";
value5[1] = "value52";
value5[2] = "value53";
root["key5"] = value5;
//for (Json::Value::const_iterator it = value5.begin(); it != value5.end(); it++) {
// cout << (*it).asString() << endl;
//}
string str = root.toStyledString();
cout << "Json 大小为: " << root.size() << endl;
cout << "Json 值为: " << str << endl;
for (Json::Value::const_iterator it = root.begin(); it != root.end(); it++) {
string key = it.name();
cout << "Current Json key is: " << key << endl;
Json::Value value = *it;
cout << "Current Json value is: " << value << endl;
cout << "Current Json value size is: " << value.size() << endl;
cout << endl;
}
return 0;
}
执行结果: