zoukankan      html  css  js  c++  java
  • c++ 解析json

     c++解析json有很库,boost,jsoncpp等。

    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,"	"));
        copy(vecAge.begin(),vecAge.end(),ostream_iterator<int>(cout,"	"));
        copy(vecName.begin(),vecName.end(),ostream_iterator<string>(cout,"	"));
        
    }

    一篇文章:

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


    jsoncpp 是比较出名的 C++ JSON 解析库。在 JSON官网也是首推的。下载地址为:http://sourceforge.net/projects/jsoncpp 。新的地址:https://github.com/open-source-parsers/jsoncpp。

  • 相关阅读:
    27 Spring Cloud Feign整合Hystrix实现容错处理
    26 Spring Cloud使用Hystrix实现容错处理
    25 Spring Cloud Hystrix缓存与合并请求
    24 Spring Cloud Hystrix资源隔离策略(线程、信号量)
    23 Spring Cloud Hystrix(熔断器)介绍及使用
    22 Spring Cloud Feign的自定义配置及使用
    21 Spring Cloud使用Feign调用服务接口
    20 Spring Cloud Ribbon配置详解
    19 Spring Cloud Ribbon自定义负载均衡策略
    18 Spring Cloud Ribbon负载均衡策略介绍
  • 原文地址:https://www.cnblogs.com/youxin/p/4911732.html
Copyright © 2011-2022 走看看