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