C + + Library---JSON

Source: Internet
Author: User
Tags string to json

The C + + operation JSON requires a separate library file, rather than directly $data = Array () as PHP does.

So the first to download the C + + JSON library file, and then the syntax to be respectful to the implementation of C + + JSON additions and deletions and other operations.

1. Add a JSON

Json::value root    // New JSON data the type of root,root can be int, string, obj, array ...

2. Adding data in JSON

 root.append (12345  ); Output: [12345]root[  key  Span style= "color: #800000;" > "].append ("  this is routine array in key     ); //  add a regular array, output: {"key": [This is routine array in key]}  root[   Key  ]="   Add a relation array   " //  add associative array, output: {"key": "Add a Relation array"}  

3. Json->string (write converts JSON value to string)

Jsoncpp in the Json::write is a pure virtual class, can not be used directly, only call his subclass, Json::fastwrite (normal output) and Json::stylewrite (formatted output)

Json::value admin;admin["Admin_key"]=555; Json::fastwriter Fast_write;cout<<fast_write.write (Admin) <<Endl;//output: {"Admin_key": 555}Json::value admin;admin["Admin_key"]=555; Json::stylewriter Style_write;cout<<style_write.write (Admin) <<Endl;//Output://{//"Admin_key": 555//}

4. String->json (read writes string to JSON value)

json::value root;std:: string " {\ "key\": \ "This is Value\"} " ; Json::reader Reader;reader.parse (abc,root); Json::styledwriter Style_write;

Note: If there is no key in the JSON array, but the key is accidentally used (called), the system will automatically add a key:null

json::value admin;admin["admin_key"]=555; int bbb = admin["key"].asint ();    // output Admin with the following result: {"Admin_key": 555, "key": null}

So when you use key, you must determine whether the key exists

 if  (Root.ismember ( " key  ) {//  There is no  cout<< exist  Span style= "color: #800000;" > " <<ENDL;}  else  {cout  << " no-exist   <<endl;}  
if (!root["key"].isnull ())       // judgment exists No, does not exist automatically creates a {    cout<<"exist"<<Endl;} Else {    cout<<"no-exist but I'll create one,the value is null"< <Endl;}    

C + + Library---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.