Windows GCC 编译 JsonCpp

下载JsonCpp https://github.com/open-source-parsers/jsoncpp/releases

安装Python 2.7.12
安装SCons 2.5.0 http://scons.org/pages/download.html
cd jsoncpp-1.7.7

jsoncpp-1.7.7>python C:\Python27\Scripts\scons.py platform=mingw

会生成静态库 .\buildscons\mingw\src\lib_json\libjson_mingw_libmt.a

cd .\buildscons\mingw\src\lib_json\
json_reader.o
json_value.o
json_writer.o
libjson_mingw_libmt.a

拷贝 include 和 buildscons\mingw\src\lib_json\ 目录到项目目录下

如果使用的是Clion,修改CMakeLists.txt,添加
target_link_libraries(net ${CMAKE_SOURCE_DIR}/lib_json/libjson_mingw_libmt.a)

VS2015需要设置 /wd4996

测试程序:

#include 
#include "include/json/json.h"

using namespace std;

int main(int argc, char * argv[])
{
//string to obj
    string strValue = "{\"key1\":\"value1\",\"array\":[{\"key2\":\"value2\"},{\"key2\":\"value3\"},{\"key2\":\"value4\"}]}";
    Json::Value v;
    Json::Reader reader;
    if(reader.parse(strValue, v))
    {
        string out =  v["key1"].asString();
        cout << out << endl;
        const Json::Value arrayObj = v["array"];
        for(int i = 0; i < arrayObj.size(); i ++)
        {
            out = arrayObj[i]["key2"].asString();
            cout << out ;
            if(i != arrayObj.size()-1)
                cout << endl;
        }
    }
    cout << endl;

//obj to string
    Json::Value root;
    Json::Value arrayObj;
    Json::Value item;
    for(int i = 0; i < 10 ; i ++)
    {
        item["key"] = i;
        arrayObj.append(item);
    }
    root["key1"] = "value1";
    root["key2"] = "value2";
    root["array"] = arrayObj;
    //string out = root.toStyledString();
    //cout << out <

编译运行