Boost Property_tree Parsing JSON

Source: Internet
Author: User
Tags parse string

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:

    1. {
    2. "Code": 0,
    3. "Images":
    4. [
    5. {
    6. "url": "Fmn057/20111221/1130/head_kjoo_05d9000251de125c.jpg"
    7. },
    8. {
    9. "url": "Fmn057/20111221/1130/original_kjoo_05d9000251de125c.jpg"
    10. }
    11. ]
    12. }
{  "code": 0,  "Images":  [    {      "url": "Fmn057/20111221/1130/head_kjoo_05d9000251de125c.jpg"    },    {      "url": "Fmn057/20111221/1130/original_kjoo_05d9000251de125c.jpg"    }  ]}
  1. int Parsejson ()
  2. {
  3. std::string str = "{\" code\ ": 0,\" images\ ": [{\" url\ ": \" Fmn057/20111221/1130/head_kjoo_05d9000251de125c.jpg\ "}, {\ "url\": \ "Fmn057/20111221/1130/original_kjoo_05d9000251de125c.jpg\"}]} ";
  4. using namespace boost::p roperty_tree;
  5. Std::stringstream SS (str);
  6. Ptree pt;
  7. try{
  8. Read_json (SS, PT);
  9. }
  10. catch (Ptree_error & E) {
  11. return 1;
  12. }
  13. try{
  14. int code = pt.get<int> ("code"); //Get the value of "code"
  15. Ptree Image_array = Pt.get_child ("Images"); //Get_child Get Array Object
  16. //Traversal array
  17. Boost_foreach (boost::p roperty_tree::p tree::value_type &v, Image_array)
  18. {
  19. Std::stringstream s;
  20. Write_json (S, V.second);
  21. std::string Image_item = S.str ();
  22. }
  23. }
  24. catch (Ptree_error & E)
  25. {
  26. return 2;
  27. }
  28. return 0;
  29. }
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

  1. int Insertjson ()
  2. {
  3. std::string str = "{\" code\ ": 0,\" images\ ": [{\" url\ ": \" Fmn057/20111221/1130/head_kjoo_05d9000251de125c.jpg\ "}, {\ "url\": \ "Fmn057/20111221/1130/original_kjoo_05d9000251de125c.jpg\"}]} ";
  4. using namespace boost::p roperty_tree;
  5. Std::stringstream SS (str);
  6. Ptree pt;
  7. try{
  8. Read_json (SS, PT);
  9. }
  10. catch (Ptree_error & E) {
  11. return 1;
  12. }
  13. //Modify/Add a key-value,key does not exist then increase
  14. Pt.put ("Upid", "00001");
  15. //Insert an array
  16. Ptree Exif_array;
  17. Ptree array1, Array2, array3;
  18. Array1.put ("make", "NIKON");
  19. Array2.put ("DateTime", "2011:05:31 06:47:09");
  20. Array3.put ("Software", "ver.1.01");
  21. Exif_array.push_back (Std::make_pair ("", array1));
  22. Exif_array.push_back (Std::make_pair ("", Array2));
  23. Exif_array.push_back (Std::make_pair ("", Array3));
  24. Exif_array.push_back (Std::make_pair ("Make", "NIKON"));
  25. Exif_array.push_back (Std::make_pair ("DateTime", "2011:05:31 06:47:09"));
  26. Exif_array.push_back (Std::make_pair ("Software", "ver.1.01"));
  27. Pt.put_child ("ExIFS", Exif_array);
  28. Std::stringstream S2;
  29. Write_json (S2, PT);
  30. std::string outstr = S2.str ();
  31. return 0;
  32. }
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

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.