Use C ++ to test the JSON third-party library JsonCpp

Source: Internet
Author: User

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:

 
 
  1. {  
  2. "name": "json″,  
  3. "array": [  
  4. {  
  5. "cpp": "jsoncpp" 
  6. },  
  7. {  
  8. "java": "jsoninjava" 
  9. },  
  10. {  
  11.  "php": "support" 
  12. }  
  13. ]  

The anti-serial number code for json is as follows:

 
 
  1. voidreadJson() {  
  2. usingnamespacestd;  
  3. std::stringstrValue = "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}";  
  4. Json::Reader reader;  
  5. Json::Value value;  
  6. if(reader.parse(strValue, value))  
  7. {  
  8. std::stringout= value["name"].asString();  
  9. std::cout <<out<<std::endl;  
  10. constJson::Value arrayObj = value["array"];  
  11.  for(unsigned inti = 0;i <arrayObj.size(); i++)  
  12. {  
  13. if(!arrayObj[i].isMember("cpp"))  
  14. continue;  
  15. out= arrayObj[i]["cpp"].asString();  
  16. std::cout <<out;  
  17. if(i != (arrayObj.size() - 1))  
  18. std::cout <<std::endl;  
  19.  }  
  20. }  

Serialize A Json object

 
 
  1. voidwriteJson() {  
  2. usingnamespacestd;  
  3. Json::Value root;  
  4. Json::Value arrayObj;  
  5. Json::Value item;  
  6. item["cpp"] = "jsoncpp";  
  7. item["java"] = "jsoninjava";  
  8. item["php"] = "support";  
  9. arrayObj.append(item);  
  10. root["name"] = "json";  
  11. root["array"] = arrayObj;  
  12. root.toStyledString();  
  13. std::stringout= root.toStyledString();  
  14. std::cout <<out<<std::endl;  
  15.  } 

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

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.