码迷,mamicode.com
首页 > 编程语言 > 详细

c++ 解析json

时间:2015-10-26 18:29:59      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:

 

boost解析json

要包含头文件:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

 

/*
g++ -o json_test json_test.cpp 
*/
#include<boost/property_tree/ptree.hpp>
#include<boost/property_tree/json_parser.hpp>

#include<string>
#include<sstream>
#include<fstream>
using namespace std;

using namespace boost::property_tree;
void create_json()
{
     ptree pt_1,pt_11,pt_12;
    
    pt_11.put("id","3445");
    pt_11.put("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;
/* 生成文件 ofstream.h

ofstream myOfstream("json.txt");
myOfstream<<os.str();
myOfstream.close();

*/
     
}
int main()
{
     
    create_json();
}

输出:

 

{
    "data": [
        {
            "id": "3445",
            "age": "29",
            "name": "chen"
        },
        {
            "id": "3445",
            "age": "29",
            "name": "chen"
        }
    ]
}

解析上面的:

void parse_json()
{
    ifstream in("json.txt");
    vector<string> vecId;
    vector<int> vecAge;
    vector<string> vecName;
    
    ptree pt,p1,p2;
    read_json<ptree>(in,pt);
    p1=pt.get_child("data");
    for(ptree::iterator it=p1.begin();it!=p1.end();it++)
    {
        p2=it->second;//first为空
        string idStr=p2.get<string>("id");
        vecId.push_back(idStr);
        
        int age=p2.get<int>("age");
        vecAge.push_back(age);
        
        string nameStr=p2.get<string>("name");
        vecName.push_back(nameStr);
        
    }
    
    copy(vecId.begin(),vecId.end(),ostream_iterator<string>(cout,"\t"));
    copy(vecAge.begin(),vecAge.end(),ostream_iterator<int>(cout,"\t"));
    copy(vecName.begin(),vecName.end(),ostream_iterator<string>(cout,"\t"));
    
}

一篇文章:

接着,将上面这串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;

下面的程序是解析Json

 

    stream << strJson;
    read_json<ptree>( stream, pt);
    p1 = pt.get_child("people");
    for (ptree::iterator it = p1.begin(); it != p1.end(); ++it)
    {
        p2 = it->second; //first为空
        stra = p2.get<string>("firstName");
        vecStr.push_back(stra);   
    }

这样,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

有问题可以留言。

 http://blog.csdn.net/yqmfly/article/details/6914590

更多:

http://blog.csdn.net/dotphoenix/article/details/27081377

c++ 解析json

标签:

原文地址:http://www.cnblogs.com/youxin/p/4911732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!