JSON is a lightweight data definition format. It is easier to learn and use than XML, and its extended functions are no worse than XML. Using JSON for data exchange is a good choice.
JSON is called JavaScript Object Notation. As the name suggests, JSON is used to mark javascript objects. For details, see http://www.json.org /.
This article chooses a third-party library JsonCpp to parse json. JsonCpp is a well-known c ++ Parsing Library, which is also the first on the json official website.
Introduction to JsonCpp
JsonCpp mainly contains three types of class: Value Reader Writer.
All objects and class names in jsoncpp are in namespace json, including json. h.
Note: Json: Value can only process ANSI strings. If the C ++ program uses Unicode encoding, it is best to add an Adapt class for adaptation.
Download and compile
The running environment is: Redhat 5.5 + g ++ version 4.6.1 + GNU Make 3.81 + jsoncpp-0.5.0
Yes: http://sourceforge.net/projects/jsoncpp/
Decompress the package and get the jsoncpp-src-0.5.0 folder. We only need the header file and cpp file of jsoncpp, where the header file of jsonscpp is located in the jsoncpp-src-0.5.0includejson and the cpp file of jsoncpp is located in the jsoncpp-src-0.5.0srclib_json.
Here I will list our working directories:
Jsoncpp // working directory
| -- Include // the root directory of the header file
| -- Json // json header file, which corresponds to the jsoncpp-src-0.5.0includejson
| -- Src // cpp source code file root directory
| -- Json // jsoncpp source code file, corresponding to the jsoncpp-src-0.5.0srclib_json
| -- Main. cpp // Our main function calls the sample code of jsoncpp.
| -- Makefile // makefile
Deserialization of Json objects
Suppose there is a json object as follows:
- {
- "name": "json″,
- "array": [
- {
- "cpp": "jsoncpp"
- },
- {
- "java": "jsoninjava"
- },
- {
- "php": "support"
- }
- ]
- }
The anti-serial number code for json is as follows:
- voidreadJson() {
- usingnamespacestd;
- std::stringstrValue = "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}";
- Json::Reader reader;
- Json::Value value;
- if(reader.parse(strValue, value))
- {
- std::stringout= value["name"].asString();
- std::cout <<out<<std::endl;
- constJson::Value arrayObj = value["array"];
- for(unsigned inti = 0;i <arrayObj.size(); i++)
- {
- if(!arrayObj[i].isMember("cpp"))
- continue;
- out= arrayObj[i]["cpp"].asString();
- std::cout <<out;
- if(i != (arrayObj.size() - 1))
- std::cout <<std::endl;
- }
- }
- }
Serialize A Json object
- voidwriteJson() {
- usingnamespacestd;
- Json::Value root;
- Json::Value arrayObj;
- Json::Value item;
- item["cpp"] = "jsoncpp";
- item["java"] = "jsoninjava";
- item["php"] = "support";
- arrayObj.append(item);
- root["name"] = "json";
- root["array"] = arrayObj;
- root.toStyledString();
- std::stringout= root.toStyledString();
- std::cout <<out<<std::endl;
- }
Download complete code
Click here to download
After the download, run the following command:
Unzip jsoncpp.zip
Cd jsoncpp
Make
./Main
Original article: http://www.cnblogs.com/ggjucheng/archive/2012/01/03/2311107.html