jsoncpp是c++的一个json解析库
下载地址 https://github.com/open-source-parsers/jsoncpp
1、准备开发环境Python scons
首先下载Python 2.7+,安装,并且加入到path
然后下载 http://scons.org/pages/download.html
我下载的是
- Zip file scons-2.4.1.zip
官网解释(The Gzip tar file and Zip file are installable using the Python setup.py
script in the package.)
下载完成以后 进入 scans 目录
查看 redeme.txt
INSTALLATION
============
Assuming your system satisfies the installation requirements in the
previous section, install SCons from this package simply by running the
provided Python-standard setup script as follows:
# python setup.py install
By default, the above command will do the following:
-- Install the version-numbered "scons-2.4.1" and "sconsign-2.4.1"
scripts in the default system script directory (/usr/bin or
C:\Python*\Scripts, for example). This can be disabled by
specifying the "--no-version-script" option on the command
line.
所以执行以下命令
F:\scons-2.4.1 python setup.py install
左右是会把 scons.py scons-2.4.1.py sconsign.py sconsign-2.4.1.py scons-time.py scons-time-2.4.1.py 等脚本 安装到 E:\java\Python27\Scripts 目录下
二、编译
查看jsoncpp的github介绍 。。。
Building and testing with SCons
Note: The SCons-based build system is deprecated. Please use CMake; see the section above.
JsonCpp can use Scons as a build system. Note that SCons requires Python to be installed.
Invoke SCons as follows:
scons platform=$PLATFORM [TARGET]
where $PLATFORM may be one of:
suncc: Sun C++ (Solaris)
vacpp: Visual Age C++ (AIX)
mingw
msvc6: Microsoft Visual Studio 6 service pack 5-6
msvc70: Microsoft Visual Studio 2002
msvc71: Microsoft Visual Studio 2003
msvc80: Microsoft Visual Studio 2005
msvc90: Microsoft Visual Studio 2008
linux-gcc: Gnu C++ (linux, also reported to work for Mac OS X)
If you are building with Microsoft Visual Studio 2008, you need to set up the environment by running vcvars32.bat (e.g. MSVC 2008 command prompt) before running SCons.
在jsoncpp的根目录执行如下命令进行编译
E:\git\jsoncpp>scons platform=mingw
然后会生成一个静态库
jsoncpp目录 --- buildscons\mingw\src\lib_json
这个目录下有四个文件
json_reader.o
json_value.o
json_writer.o
libjson_mingw_libmt.a
把整个目录全部拷贝到工程目录的跟目录下
然后按照如下步骤配置一下静态库
编译的时候会执行如下命令
g++ -DCURL_STATILIB -O3 -Wall -c -fmessage-length=0 -o "src\\main.o" "..\\src\\main.cpp"
g++ "-LE:\\git\\jsoncpp\\buildscons\\mingw\\src\\lib_json" -o http-c.exe "src\\main.o" -ljson_mingw_libmt
-ljson_mingw_libmt 是使用到的静态库。。。。
具体需要查一下g++的用法。。。。
然后简单写一个demo
std::string strValue = "{\"key1\":\"value1\",\"array\":[{\"key2\":\"value2\"},{\"key2\":\"value3\"},{\"key2\":\"value4\"}]}";
Json::Reader reader;
Json::Value value;
if (reader.parse(strValue, value)) {
std::string out = value["key1"].asString();
std::cout << out << std::endl;
// const Json::Value arrayObj = value["array"];
// for (int i = 0; i < arrayObj.size(); i++) {
// out = arrayObj[i]["key2"].asString();
// std::cout << out;
// if (i != arrayObj.size() – 1){
// std::cout << std::endl;
// }
// }
}