采用boost中的property_tree库操作json,xml(vs2008,unicode)

前段时间,采用了jsoncpp操作json,这几天在做总结,然后查询到通过boost也可以完成。至于boost的介绍,大家可以搜索了解。

首先下载boost,boost_1_53_0

然后就是编译了

1. 下载

2. 解压缩后,运行bootstrap.bat批处理文件,得到bjam.exe;

3. 打开vs2008,工具-外部工具,里面可以看到“Visual Studio 2008 Command Prompt”,那么我们模仿这个新建一个,参数可以为空,初始目录改为bjam.exe所在的目录。然后在工具菜单下就可以看到自己刚刚新建的工具了,点击他。(这里做的目的就是想采用命令参数的方式运行bjam,不过我用其他方式都没成功,就用这种笨办法了)。

4. 输入“bjam --toolset=msvc-9.0 architecture=x86 link=static runtime-link=static threading=multi debug release --with-property_tree --with-program_options”后,完成编译。编译成的lib文件,放在stage\lib下,形如“libboost_program_options-vc90-sgd-1_53.lib”.

其中:msvc-9.0代表编译成VC9.0(即VS2008)版本,对应的msvc-10.0代表编译成VC10.0(即VS2010)版本; address-model=64代表编译成64位版本,如省略此项,默认为编译成32位版本,--with表示要编译的库,--build-type=complete stage表示编译全部;link=static runtime-link=static表示生成静态库

ps:可以直接运行bjam.exe进行编译,会自动检查相关项,不过生成的库形如“libboost_program_options-vc90-gd-1_53.lib”

编译的时候,很多人遇到了这个错误LINK : fatal error LNK1104: 无法打开文件“libboost_program_options-vc90-mt-sgd-1_53.lib”

那就是由于编译选项里面没有指定静态,所以导致生成的库不对。

库可以用了,那我们就开始测试了

1.包含boost文件和库

文件目录..\boost_1_53_0

库目录..\boost\boost_1_53_0\stage\lib

2.添加需要的头文件

#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
#include "boost/property_tree/xml_parser.hpp"

//#include "boost/program_options/detail/convert.hpp"
#include "boost/program_options/detail/utf8_codecvt_facet.hpp"

3.主角

try
    {
        /* create boost utf8 codecvt */
        std::locale oldLocale;
        //指定了内部码和外部码 内部unicode,外部utf-8
        std::locale utf8Locale(oldLocale,
            new boost::program_options::detail::utf8_codecvt_facet());

        /* create the wptree for save the UTF-8 data */
        boost::property_tree::wptree datum;
        datum.put(L"root.num", 100);
        datum.put(L"root.str", L"abc中文字符串");

        /* output XML string */
        std::wostringstream xmlOutputStream;
        xmlOutputStream.imbue(utf8Locale);
        boost::property_tree::xml_parser::write_xml(xmlOutputStream,
            datum);

        /* output JSON string */
        std::wostringstream jsonOutputStream;
        jsonOutputStream.imbue(utf8Locale);
        boost::property_tree::json_parser::write_json(jsonOutputStream,
            datum);

        /* read datum from JSON stream */
        boost::property_tree::wptree wptParse;
        std::wistringstream jsonIStream;
        jsonIStream.imbue(utf8Locale);
        jsonIStream.str(jsonOutputStream.str());
        boost::property_tree::json_parser::read_json(jsonIStream,
            wptParse);
        int num = wptParse.get<int>(L"root.num");
        std::wstring wstrVal = wptParse.get<std::wstring>(L"root.str");
        
        CString strMsg;
        strMsg.Format(_T("XML format:%s\nJSON format:%s\n个数,%d字符串%s")
            ,xmlOutputStream.str().c_str(),jsonOutputStream.str().c_str()
            ,num,wstrVal.c_str());
        MessageBox(NULL,strMsg,_T("提示"),MB_OK);
    }
    catch (...)
    {
        MessageBox(NULL,_T("create boost::property_tree::wptree failed\n"),_T("提示"),MB_OK);
    }

由于我们的工程是unicode,但是boost支持的是utf-8,所以就需要utf8_codecvt_facet来进行转换。这个测试使用的过程,发现解析json的时候没问题,但是输出json串会有乱码的问题。

参考文章:http://blog.csdn.net/great3779/article/details/6977922/

http://blog.csdn.net/harbinzju/article/details/6631738

http://blog.sina.com.cn/s/blog_5218d56201009hzh.html

原文地址:https://www.cnblogs.com/junyuz/p/3094559.html