Recently started using Jsoncpp, used to always use Cjson, but when using cjson often forget the free JSON memory, resulting in a memory leak, the program ran and ran crashed. So recently transferred JSON to the Jsoncpp.
Jsoncpp code source file is placed in the Baidu network disk: Http://pan.baidu.com/s/1ntqQhIT or Jsoncpp.zip
Here is the Jsoncpp source code in the test instance, you feel ah.
1#include <string>2#include <json/json.h>3 4 voidReadjson ();5 voidWritejson ();6 7 intMainintargcChar**argv) {8 Readjson ();9 Writejson ();Ten return 0; One } A - voidReadjson () { - using namespacestd; theSTD::stringstrvalue ="{\ "name\": \ "json\", \ "array\": [{\ "cpp\": \ "Jsoncpp\"},{\ "java\": \ "Jsoninjava\"},{\ "php\": \ "Support\"}]} "; - - Json::reader Reader; - Json::value Value; + - if(Reader.parse (strvalue, value)) + { ASTD::string out= value["name"].asstring (); atStd::cout << out<<Std::endl; - ConstJson::value arrayobj = value["Array"]; - for(unsignedinti =0; I < arrayobj.size (); i++) - { - if(!arrayobj[i].ismember ("CPP")) - Continue; in out= arrayobj[i]["CPP"].asstring (); -Std::cout << out; to if(I! = (Arrayobj.size ()-1)) +Std::cout <<Std::endl; - } the } * } $ Panax Notoginseng voidWritejson () { - using namespacestd; the + Json::value Root; A Json::value arrayobj; the json::value Item; + -item["CPP"] ="Jsoncpp"; $item["Java"] ="Jsoninjava"; $item["PHP"] =" Support"; - arrayobj.append (item); - theroot["name"] ="JSON"; -root["Array"] =arrayobj;Wuyi the root.tostyledstring (); -STD::string out=root.tostyledstring (); WuStd::cout << out<<Std::endl; -}
Here is the result of the run
1 JSON2 Jsoncpp3 {4 "Array" : [5 {6 "CPP":"Jsoncpp",7 "Java":"Jsoninjava",8 "PHP":" Support"9 }Ten ], One "name":"JSON" A}
Storm
Mail: [Email protected]
Jsoncpp Code Instance