JSON

boost ѧϰ±Ê¼Ç 7£ºproperty

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-03-16 18:00 ÎÒÒªÆÀÂÛ( )

ÀûÓÃboost ÖÐµÄ property_tree½âÎö jsonºÍ xml

ÉÏһƪÎÄÕÂboostÈÝÆ÷ÖÐÁôÏÂÒ»¸öproperty_tree ûÓÐѧ£¬¾õµÃ¼ÈÈ» boost ÌṩÁË property_tree ÕâÑùºÃµÄ¹¤¾ßÀ´¸øC++ ½âÎöxmlºÍJson£¬±ØÐëÒªÁôÓÐÒ»¶¨µÄƪ·ùÀ´½²Ëü¡£

Ô­ÏÈÏîÄ¿ÖÐʹÓõ½JSON£¬ÐèҪʹÓÃC++½âÎöÒ»¶Î·µ»ØÖµ, JSON ¸ñʽÈçÏ£º

error ×Ö¶ÎÊÇÒ»¸öÊý×飬Êý×éÖеÄÿ¸öÔªËض¼ÊÇÒ»¸ö¶ÔÏóObject£¬Ã¿¸öObjectÖÐÊÇÒ»¸ö¼üÖµ¶Ô£¬ÆäÖÐ errorstroke ͬÑù°üº¬Ò»¸ö¶ÔÏó¡£ÎÒÃǶ¼ÖªµÀJSONÖ»°üº¬ÈýÖÖÊý¾Ý½á¹¹£¬Ê¸Á¿£¬Êý×éºÍÓ³É䣨²Î¿¼£©£¬ÕâÑùÎÞÒɸøÎÒÃǵĽâÎö¹¤×÷´øÀ´ºÜ¶à±éÀú£¬ÕâÈýÖÖÊý¾Ý½á¹¹¼¸ºõ¿ÉÒÔ½«Ê±¼äËùÓеÄÐÅÏ¢°üº¬½øÈ¥¡£

ÏÂÃæ¾ÍÊǽâÎöÕâ¶Î JSON ×Ö·û´®µÄ¾ßÌå´úÂ룺£¨×¢Ò⽫JSON×Ö·û´® escape£©

#include <iostream> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <boost/foreach.hpp> #include <string> using namespace boost::property_tree; int main(int argc, const char * argv[]) { std::string str_json = "{\"ret\":\"101\",\"error\":[{\"errortype\":\"A0001\",\"errorstroke\":{\"0\":\"0.2\",\"1\":\"0.3\"}},{\"errortype\":\"A0021\",\"errorstroke\":{\"0\":\"0.2\",\"1\":\"0.3\"}}]}"; ptree pt; //define property_tree object std::stringstream ss(str_json); try { read_json(ss, pt); //parse json } catch (ptree_error & e) { return 1; } std::cout << pt.get<std::string>("ret") << std::endl; ptree errortype = pt.get_child("error"); // get_child to get errors // first way for (boost::property_tree::ptree::iterator it = errortype.begin(); it != errortype.end(); ++it) { std::cout << it->first; std::cout << it->second.get<std::string>("errortype") << std::endl; ptree errorstroke = it->second.get_child("errorstroke"); for (ptree::iterator iter = errorstroke.begin(); iter != errorstroke.end(); ++iter) { std::string key = iter->first; std::cout << iter->first << std::endl; std::cout << iter->second.data() << std::endl; } } // second way: using boost foreach feature // BOOST_FOREACH(ptree::value_type &v, errortype){ // ptree& childparse = v.second; // std::cout << childparse.get<std::string>("errortype") << std::endl; // ptree errorstroke = childparse.get_child("errorstroke"); // BOOST_FOREACH(ptree::value_type& w, errorstroke){ // std::cout << w.first << std::endl; // std::cout << w.second.data() << std::endl; // } // } return 0; }

´úÂëµÄÊä³ö£º

101 A0001 0 0.2 1 0.3 A0021 0 0.2 1 0.3

ÔÙ»»ÓÃÁíÍâÒ»¶Î JSON ³¢ÊÔ½âÎö£¬´ÓÏÂÃæÕâ¶Î JSON ÖÐÄܹ»ÇáÒ׵Ŀ´³ö JSON ÄÚ²¿µÄ½á¹¹£¬Ö§³ÖµÄÊý¾Ý½á¹¹ºÍÀàÐÍ£º

¾ßÌå½âÎö´úÂ룺

#include <iostream> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <boost/foreach.hpp> #include <string> using namespace std; using namespace boost::property_tree; int main(int argc, const char * argv[]) { std::string str_json = "{\"array\":[1,2,3],\"boolean\":true,\"null\":null,\"number\":123,\"object\":{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\"},\"string\":\"Hello World\"}"; ptree pt; //define property_tree object std::stringstream ss(str_json); try { read_json(ss, pt); //parse json } catch (ptree_error & e) { return 1; } ptree arraypt =pt.get_child("array"); for (boost::property_tree::ptree::iterator it = arraypt.begin(); it != arraypt.end(); ++it) { cout << it->second.data() << " "; } cout << endl; std::cout << pt.get<bool>("boolean") << std::endl; std::cout << pt.get<std::string>("null") << std::endl; std::cout << pt.get<int>("number") << std::endl; std::cout << pt.get<std::string>("string") << std::endl; ptree opt = pt.get_child("object"); BOOST_FOREACH(ptree::value_type &v, opt){ cout << v.first << " : "; cout << v.second.data() << endl; } return 0; }

ÀûÓà Boost property_tree ¹¹Ôì JSON ×Ö´®,ÒÔÏ´úÂëÄܹ»¹¹ÔìÉÏÃæµÄJSON£º

#include <iostream> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <boost/foreach.hpp> #include <string> using namespace std; using namespace boost::property_tree; int main(int argc, const char * argv[]) { std::string str_json = "{\"array\":[1,2,3],\"boolean\":true,\"null\":null,\"number\":123,\"object\":{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\"},\"string\":\"Hello World\"}"; ptree root, arr,object; arr.push_back(std::make_pair("","1")); arr.push_back(std::make_pair("","2")); arr.push_back(std::make_pair("","3")); object.put("a","b"); object.put("c","d"); object.put("e","f"); root.add_child("array", arr); bool boolvalue = true; root.put("boolean",boolvalue); root.put("null","null"); int num = 123; root.put("number",num); root.add_child("object",object); root.put("string","Hello World"); //write_json("out.json", root); stringstream s; write_json(s, root, false); string out = s.str(); cout << out ; return 0; }

Á½¸öÓÐÓõÄJSON¹¤¾ß£º

  • JSON ÔÚÏ߱༭
  • JSON Escape and Unescape ¹¤¾ß
  • Boost property_tree ½âÎö XML ¿É²Î¿¼ÕâƪÎÄÕ £¬½âÊ͵÷dz£Çå³þ¡£

    ¡¡

    1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

    Ïà¹ØÎÄÕÂ
    • Êý¾Ý·ÖÎöʦѧϰʵÀý£ºPythonÀ­¹´ÅÀ³æ

      Êý¾Ý·ÖÎöʦѧϰʵÀý£ºPythonÀ­¹´ÅÀ³æ

      2017-03-16 16:05

    • Android±Ê¼Ç¡ª¡ªÊ²Ã´ÊÇjson?jsonÈçºÎʹÓÃ?

      Android±Ê¼Ç¡ª¡ªÊ²Ã´ÊÇjson?jsonÈçºÎʹÓÃ?

      2017-03-15 13:00

    • Spring MVC Rest ѧϰ Ò»

      Spring MVC Rest ѧϰ һ

      2017-03-12 11:04

    • UE4ѧϰ:ÏÂÔØÎļþ²¢×ª³ÉJSONÀàÐÍÊý¾Ý

      UE4ѧϰ:ÏÂÔØÎļþ²¢×ª³ÉJSONÀàÐÍÊý¾Ý

      2017-03-12 09:00

    ÍøÓѵãÆÀ
    ¡