生成:
// test.cpp #include <iostream> #include <string> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <boost/foreach.hpp> using namespace std; using namespace boost::property_tree; int main(){ ptree pt_1,pt_11,pt_12; pt_11.put("id","3445"); pt_11.put<int>("age",29); pt_11.put("name","chen"); pt_12.push_back(make_pair("",pt_11)); pt_12.push_back(make_pair("",pt_11)); //replace or create child node "data" pt_1.put_child("data",pt_12); ostringstream os; write_json(os,pt_1); cout<<os.str()<<endl; system("pause"); return 0; } // =========== 产生如下JSON串: =============== /* { "data": [ { "id": "3445", "age": "29", "name": "chen" }, { "id": "3445", "age": "29", "name": "chen" } ] } */
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <boost/foreach.hpp>
void downgadget::Downloader_c::makereport(const downloaderInfo_t info) { using boost::property_tree::ptree; ptree downloaderInfo ; downloaderInfo.put("id",info.id); downloaderInfo.put("code",info.code); downloaderInfo.put("mesg",info.mesg); downloaderInfo.put("path",info.path); downloaderInfo.put("md5sum",info.md5sum); downloaderInfo.put("size",info.size); downloaderInfo.put("url",info.url); downloaderInfo.put("time_download",info.time_download); downloaderInfo.put("time_install",info.time_install); downloaderInfo.put("cmd_install",info.cmd_install); std::ostringstream os; write_json(os,downloaderInfo); std::cout<<os.str()<<std::endl; std::ofstream tmpReport(m_reportPath); if(!tmpReport){ std::cout<<"critical error open output report error!"<<std::endl; }else{ tmpReport<<os.str(); } tmpReport.close(); }
解析Json的方法有很多,也有不少的第三方开源工具。这里仅介绍其中的一种,用Bosst解析。Boost库是一个可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一。 Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容。在C++社区中影响甚大,是不折不扣的“准”标准库。Boost由于其对跨平台的强调,对标准C++的强调,与编写平台无关。大部分boost库功能的使用只需包括相应头文件即可,少数(如正则表达式库,文件系统库等)需要链接库。但Boost中也有很多是实验性质的东西,在实际的开发中实用需要谨慎。
鉴于Boost的强大功能,就用Boost来解析Json格式,包括简单的及复杂的。
首先给出一个Json例子。
{ "people": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" } ]}
要解析Json,需要包括头文件。
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/date_time.hpp>
还有
#include <string>
#include <vector>
#include <sstream.h>
using namespace boost::property_tree;
using namespace boost::gregorian;
using namespace boost;
接着,将上面这串Json赋给一个变量
string strJson ={ "people": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" } ]}
注:在C++中需要在“前面加入\进行转意。
接下来,给程序增加如下变量:
string stra,strc;
vector<string> vecStr;
ptree pt,p1,p2;
stringstream stream;
这样,vecStr中就有三个值,Brett,Jason,Elliotte,Json解析完成。
对于下面这样的Json格式,
{ "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }, { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" }, { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" } ], "authors": [ { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" }, { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" } ], "musicians": [ { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }, { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" } ] }
就需要对上面的这两句程序稍微改动下就行了。
p1 = pt.get_child("programmers");
stra = p2.get<string>("firstName");
对比一下,发现什么不同了吗?就是将参数稍微修改了一下。如果要得到musicians里面的firstName呢?就只要将p1 = pt.get_child("programmers");中的参数改成musicians;
如果Json里面嵌套了Json,则需要增加一个Ptree 变量pt3,pt4.使
p3 = p2.get_child("嵌套的值");
for (ptree::iterator ita = p3.begin(); ita != p3.end(); ++ita)
{
p4 = ita->second;
strc = p4.get<string>("markerItemLink");
}
相关源代码请访问地址
http://download.csdn.net/detail/yqmfly/3729591
有问题可以留言。
#include <iostream> #include <string> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <boost/foreach.hpp> using namespace std; using namespace boost::property_tree;
string json_string="{\"-f\": \"/usr/reservedfff_dir\", \"-s\": \"/usr/reservedddd_dir\"}"; string str_head; string str_node_val; ptree pt,p1,p2; stringstream stream; stream << json_string ; read_json<ptree>( stream, pt); for (ptree::iterator ita = pt.begin(); ita != pt.end(); ++ita) { cout<<"first:"<<ita->first<<endl; str_node_val = pt.get<string>(ita->first); cout<<str_node_val<<endl; }
加入exception:
string json_string="{\"-f\": \"/usr/reservedfff_dir\" \"-s\": \"/usr/reservedddd_dir\"}"; string str_head; string str_node_val; ptree pt,p1,p2; stringstream stream; stream << json_string ; try{ read_json<ptree>( stream, pt); cout<<"parsing ok\n"<<endl; for (ptree::iterator ita = pt.begin(); ita != pt.end(); ++ita) { cout<<"first:"<<ita->first<<endl; str_node_val = pt.get<string>(ita->first); cout<<str_node_val<<endl; } } catch(std::runtime_error& e) { std::cout<<e.what()<<endl; }