There are many JSON parsing methods and many third-party open-source tools. Here we only introduce one of them, using bosst resolution. The boost library is a C ++ library that can be transplanted and provide source code. As a backup for the standard library, it is one of the engines of the C ++ standardization process. The boost library is initiated by members of the C ++ Standards Committee Working Group. Some of the content is expected to become the content of the next-generation C ++ standard library. The C ++ community has a huge impact and is a "quasi" standard library. Because of its emphasis on cross-platform platforms and Standard C ++, boost has nothing to do with the writing platform. Most boost library functions only need to include the corresponding header files, and a few (such as regular expression library and file system library) need to be linked to the library. However, there are many experimental things in boost, so you need to be cautious when using it in actual development.
In view of the powerful functions of boost, boost is used to parse the JSON format, including simple and complex ones.
First, a JSON example is provided.
{"People": [{"firstname": "Brett", "lastname": "McLaughlin", "email": "aaaa "},
{"Firstname": "Jason", "lastname": "Hunter", "email": "BBBB "},
{"Firstname": "elliotte", "lastname": "Harold", "email": "CCCC"}]}
To parse JSON, the header file must be included.
# Include <boost/property_tree/ptree. HPP>
# Include <boost/property_tree/json_parser.hpp>
# Include <boost/date_time.hpp>
And
# Include <string>
# Include <vector>
# Include <sstream. h>
Using namespace boost: property_tree;
Using namespace boost: Gregorian;
Using namespace boost;
Then, assign the 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 add \ in front of "for conversion.
Next, add the following variables to the program:
String stra, STRC;
Vector <string> vecstr;
Ptree PT, P1, P2;
Stringstream stream;
The following program parses 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; // The value of first is null.
Stra = p2.get <string> ("firstname ");
Vecstr. push_back (stra );
}
In this way, the vecstr contains three values: Brett, Jason, elliotte, and JSON.
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": "Rachmaninoff", "instrument ": "Piano"}]}
You just need to make some changes to the above two statements.
P1 = pt. get_child ("programmers ");
Stra = p2.get <string> ("firstname ");
What are the differences? The parameter is slightly modified. What if I want to get the firstname in musicians? Change the parameter in P1 = pt. get_child ("programmers"); to musicians;
If JSON is nested in JSON, you need to add a ptree variable pt3, pt4.
P3 = p2.get _ child ("nested value ");
For (ptree: iterator ita = p3.begin (); ita! = P3.end (); ++ ITA)
{
P4 = Ita-> second;
STRC = p4.get <string> ("markeritemlink ");
}
Access the Source Code address
Http://download.csdn.net/detail/yqmfly/3729591
If you have any questions, leave a message.