Parsing JSON using boost property_tree
Before using Jsoncpp parsing JSON, now know that boost has a library of analysis, learn about it
Property_tree can parse data in xml,json,ini,info and other formats, and using Property_tree to parse these formats is very similar.
Parsing JSON is simple, and the namespace is boost: The:p Roperty_tree,reson_json function parses a file stream, a string into Ptree,write_json, and outputs the ptree as a string or a file stream. The rest is the operation of the Ptree.
Parsing JSON requires a header file:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
1. Parsing JSON
Parse the following data:
- {
- "Code": 0,
- "Images":
- [
- {
- "url": "Fmn057/20111221/1130/head_kjoo_05d9000251de125c.jpg"
- },
- {
- "url": "Fmn057/20111221/1130/original_kjoo_05d9000251de125c.jpg"
- }
- ]
- }
{ "code": 0, "Images": [ { "url": "Fmn057/20111221/1130/head_kjoo_05d9000251de125c.jpg" }, { "url": "Fmn057/20111221/1130/original_kjoo_05d9000251de125c.jpg" } ]}
- int Parsejson ()
- {
- std::string str = "{\" code\ ": 0,\" images\ ": [{\" url\ ": \" Fmn057/20111221/1130/head_kjoo_05d9000251de125c.jpg\ "}, {\ "url\": \ "Fmn057/20111221/1130/original_kjoo_05d9000251de125c.jpg\"}]} ";
- using namespace boost::p roperty_tree;
- Std::stringstream SS (str);
- Ptree pt;
- try{
- Read_json (SS, PT);
- }
- catch (Ptree_error & E) {
- return 1;
- }
- try{
- int code = pt.get<int> ("code"); //Get the value of "code"
- Ptree Image_array = Pt.get_child ("Images"); //Get_child Get Array Object
- //Traversal array
- Boost_foreach (boost::p roperty_tree::p tree::value_type &v, Image_array)
- {
- Std::stringstream s;
- Write_json (S, V.second);
- std::string Image_item = S.str ();
- }
- }
- catch (Ptree_error & E)
- {
- return 2;
- }
- return 0;
- }
int Parsejson () { std::string str = "{\" code\ ": 0,\" images\ ": [{\" url\ ": \" Fmn057/20111221/1130/head_kjoo_ 05d9000251de125c.jpg\ "},{\" url\ ": \" Fmn057/20111221/1130/original_kjoo_05d9000251de125c.jpg\ "}]}"; Using namespace boost::p roperty_tree; Std::stringstream SS (str); Ptree pt; try{ Read_json (SS, PT); } catch (Ptree_error & E) { return 1; } try{ int code = pt.get<int> ("code"); Get "code" of value ptree Image_array = Pt.get_child ("Images"); Get_child get Array Object //traversal array Boost_foreach (BOOST::p roperty_tree::p tree::value_type &v, Image_array) { std::stringstream s; Write_json (S, v.second); std::string Image_item = S.str (); } } catch (Ptree_error & E) { return 2; } return 0;}
2. Structuring the JSON
- int Insertjson ()
- {
- std::string str = "{\" code\ ": 0,\" images\ ": [{\" url\ ": \" Fmn057/20111221/1130/head_kjoo_05d9000251de125c.jpg\ "}, {\ "url\": \ "Fmn057/20111221/1130/original_kjoo_05d9000251de125c.jpg\"}]} ";
- using namespace boost::p roperty_tree;
- Std::stringstream SS (str);
- Ptree pt;
- try{
- Read_json (SS, PT);
- }
- catch (Ptree_error & E) {
- return 1;
- }
- //Modify/Add a key-value,key does not exist then increase
- Pt.put ("Upid", "00001");
- //Insert an array
- Ptree Exif_array;
- Ptree array1, Array2, array3;
- Array1.put ("make", "NIKON");
- Array2.put ("DateTime", "2011:05:31 06:47:09");
- Array3.put ("Software", "ver.1.01");
- Exif_array.push_back (Std::make_pair ("", array1));
- Exif_array.push_back (Std::make_pair ("", Array2));
- Exif_array.push_back (Std::make_pair ("", Array3));
- Exif_array.push_back (Std::make_pair ("Make", "NIKON"));
- Exif_array.push_back (Std::make_pair ("DateTime", "2011:05:31 06:47:09"));
- Exif_array.push_back (Std::make_pair ("Software", "ver.1.01"));
- Pt.put_child ("ExIFS", Exif_array);
- Std::stringstream S2;
- Write_json (S2, PT);
- std::string outstr = S2.str ();
- return 0;
- }
int Insertjson () {std::string str = "{\" code\ ": 0,\" images\ ": [{\" url\ ": \" Fmn057/20111221/1130/head_kjoo_ 05d9000251de125c.jpg\ "},{\" url\ ": \" Fmn057/20111221/1130/original_kjoo_05d9000251de125c.jpg\ "}]}"; Using namespace boost::p roperty_tree; Std::stringstream SS (str); Ptree pt; try{Read_json (SS, PT); } catch (Ptree_error & E) {return 1; }//Modify/Add a key-value,key does not exist then increase pt.put ("Upid", "00001"); Insert an array ptree exif_array; Ptree array1, Array2, array3; Array1.put ("Make", "NIKON"); Array2.put ("DateTime", "2011:05:31 06:47:09"); Array3.put ("Software", "ver.1.01"); Exif_array.push_back (Std::make_pair ("", array1)); Exif_array.push_back (Std::make_pair ("", array2)); Exif_array.push_back (Std::make_pair ("", array3));//Exif_array.push_back (Std::make_pair ("Make", "NIKON");//Exif_ Array.push_back (Std::make_pair ("DateTime", "2011:05:31 06:47:09");//Exif_array.push_back (Std::make_pair (" Software "," ver.1.01 ")); Pt.put_child ("ExIFS", Exif_array); Std::stringstReam S2; Write_json (S2, PT); std::string outstr = S2.str (); return 0;}
Three. Experience with two types of analytic libraries
1. With boost::p Roperty_tree parse string encountered "\" when parsing failed, and Jsoncpp can parse the success, you know '/' preceded by a ' \ ' is the JSON standard format.
2. Boost::p roperty_tree Read_json and Write_json used in multi-threading can cause crashes.
For 1, you can use boost::p Roperty_tree parsing before writing a function to remove the "\" in the "\", for 2, in multi-threaded synchronization can be resolved.
My experience: Using boost::p Roperty_tree not only can parse JSON, but also can parse data in xml,info format. For parsing json, using boost::p Roperty_tree parsing can be tolerated, but parsing XML, because there are too many problems can only be replaced by other libraries.
Boost Property_tree Parsing JSON