C + + parsing JSON

Source: Internet
Author: User

Boost parsing JSON

To include a header file:

#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 namespacestd;using namespaceboost::p roperty_tree;voidCreate_json () {Ptree Pt_1,pt_11,pt_12; Pt_11.put ("ID","3445"); Pt_11.put (" Age", in); 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;
/* Generate file Ofstream.h

Ofstream Myofstream ("Json.txt");
Myofstream<<os.str ();
Myofstream.close ();

*/     } int Main () {         Create_json ();}

Output:

{    "Data": [        {            "ID":"3445",            " Age":" in",            "name":"Chen"        },        {            "ID":"3445",            " Age":" in",            "name":"Chen"        }    ]}

Parse the above:

voidParse_json () {Ifstreaminch("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 is empty        stringIdstr=p2.Get<string> ("ID");                Vecid.push_back (IDSTR); intAge=p2.Get<int> (" Age");                Vecage.push_back (age); stringNamestr=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")); }

An article:

Next, assign the above JSON string to a variable

String Strjson ={"People": [{"FirstName": "Brett", "LastName": "McLaughlin", "email": "AAAA"},

{"FirstName": "Jason", "LastName": "Hunter", "email": "BBBB"},

{"FirstName": "Elliotte", "LastName": "Harold", "email": "CCCC"}]}

Note: in C + + you need to "precede" to "add" to the intention.

Next, add the following variables to the program:

String stra,strc;
Vector<string> Vecstr;
Ptree PT,P1,P2;
StringStream stream;

The following program is parsing 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 is empty
Stra = p2.get<string> ("FirstName");
Vecstr.push_back (Stra);
}

In this way, there are three values in the VECSTR, Brett,jason,elliotte,json parsing is completed.

For the following JSON format,

{"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": "Rachman Inoff "," Instrument ":" Piano "}]}

It is necessary to change the above two sentences slightly.

P1 = Pt.get_child ("Programmers");

Stra = p2.get<string> ("FirstName");

Compare it and find out what's different? is to modify the parameters a little bit. What if you want to get the FirstName inside musicians? As long as the P1 = Pt.get_child ("Programmers"), the parameters are changed to musicians;

If JSON is nested inside JSON, you need to add a Ptree variable PT3,PT4. Make

P3 = P2.get_child ("nested value");

for (Ptree::iterator ita = P3.begin (); ITA! = P3.end (); ++ita)
{
P4 = ita->second;
STRC = p4.get<string> ("Markeritemlink");
}

Related source code please visit address

http://download.csdn.net/detail/yqmfly/3729591

There are questions to leave a message.

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

More:

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

C + + parsing JSON

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.