jsoncpp在win7下用mingw编译

jsoncpp是一个c++封装的json包,跨平台支持windows、linux、unix等多系统。

在windows下面使用比较简单,直接往vc里面添加项目就可以了。l

如果希望用MINGW编译的库来写跨平台的一些东东的话,需要使用到README文档中的scons,

scons功能和GNU make一样,又比make简单多了。scons是python工具,需要先安装好python。

* Building/Testing:
  =================

JsonCpp uses Scons (http://www.scons.org) as a build system. Scons requires
python to be installed (http://www.python.org).

You download scons-local distribution from the following url:
http://sourceforge.net/projects/scons/files/scons-local/1.2.0/

文档中是这么写,不过scons已经到了2.1.0版了

下载scons-src-2.1.0,解压。不需要编译安装,可以直接使用源码,放在

jsoncpp的readme所在的目录下


具体到jsoncpp的编译,可以进入到jsoncpp目录,执行
python scons.py platform=mingw


生成jsoncpp-src-0.6.0-rc2\libs\mingw\libjson_mingw_libmt.a 库文件。

把include目录和.a文件放在编译环境的相应目录中

接下来可以在devcpp中开一个源文件

#include <iostream>
#include <cstdlib>
#include <string>
#include <json\json.h>
using namespace std;


int main(int argc, char * argv[])
{
    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;
    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;
    root.toStyledString();
    string out = root.toStyledString();
    cout << out <<endl;
    
    Json::FastWriter writer;
    string res = writer.write(root);
    cout << res << endl;
    
    cout << endl;
    system("pause");
    return 0;   
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值