Jsoncpp Simple to use

Source: Internet
Author: User

[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

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.