[Turn] From:
Http://www.cnblogs.com/ytjjyy/archive/2012/04/17/2453348.html
Jsoncpp is a C + +the development package used to process the JSON data. Here's how to use Jsoncpp to serialize and deserialize JSON objects, with actual code as an example. Deserialize a JSON object such as a string sequence of JSON objects, where "array": [...] Represents an array in a JSON object: {"Key1″:" Value1″, "array": [{"Key2":"value2"},{"Key2":"Value3"},{"Key2":"Value4"}]}, how do you get the values for Key1 and Key2, and the code looks like this: std::stringstrvalue = "{\" key1\ ": \" value1\ ", \" array\ ": [{\"key2\ ": \" value2\ "},{\" key2\ ": \" value3\ "},{\" key2\ ": \" Value4\ "}]}"; Json::reader Reader; Json::value Value; if(Reader.parse (strvalue, value)) {std::string out= value["Key1"].asstring (); Std::cout<< out<<Std::endl; ConstJson::value arrayobj = value["Array"]; for(intI=0; I<arrayobj.size (); i++) { out= arrayobj[i]["Key2"].asstring (); Std::cout<< out; if(I! = Arrayobj.size () –1) Std::cout<<Std::endl; }} to serialize the JSON object to first build a JSON object that contains an array, and then serialize the JSON object into a string with the following code: Json::value root; Json::value arrayobj; Json::value item; for(intI=0; i<Ten; i++) {item["Key"] =i; Arrayobj.append (item); } root["Key1"] ="Value1″; root["Key2"] ="Value2″; root["Array"] =arrayobj; Root.tostyledstring (); STD::string out=root.tostyledstring (); Std::cout<< out<<Std::endl; To delete a JSON object std::stringStrcontent ="{\ "key\": \ "1\", \ "name\": \ "test\"}"; Json::reader Reader; Json::value Value; if(Reader.parse (strcontent, value)) {Json::value root=value; Root.removemember ("Key"); printf ("%s \ n", Root.tostyledstring (). C_STR ());
Jsoncpp Simple to use