A good article on C ++ parsing JSON

Source: Internet
Author: User
Use C ++ to process JSON data exchange formats
 
I. Summary
 
JSONJSON is used to mark JavaScript objects. JSON is a lightweight data transmission format. This article does not detail the details of JSON, and aims to discuss how to use C ++ to process JSON. For more information about JSON, see the JSON Official Website: http://www.json.org. Http://json.org/json-zh.html
 
2. Select the C ++ library for processing JSON
 
This article chooses a third-party library jsoncpp to parse JSON. Jsoncpp is a well-known C ++ JSON Parsing Library. It was also the first on the JSON official website. Http://sourceforge.net/projects/jsoncpp. The jsoncpp version used in this article is 0.5.0.
 
3. jsoncpp compilation in Windows
 
To use a third-party source code library, the first step is to compile the source code file into a convenient dynamic link library, static link library, or static import library [1].
 
The source code files for JSON parsing by jsconcpp are distributed under include/JSON and src/lib_json. In fact, there are not many jsoncpp source code. In order to facilitate product management, it is unnecessary to compile it as a dynamic link library or static import library. Therefore, we choose to use the static Link Library [2].
 
Jsoncpp has been well processed, and all compilation options have been configured. Open makefiles/vs71/jsoncpp. SLN can start compilation (by default, the vs2003 compiler is used. When it is enabled, it can be converted directly according to the vs2005 prompt ).
 
Iv. jsoncpp usage
JsoncppIt mainly includes three types of Class: value, reader, and writer. All objects and class names in jsoncpp are in namespace JSON, including JSON. h.
 
JSON: ValueOnly strings of the ANSI Type can be processed.ProgramIt is unicode encoded. It is best to add an adapt class for adaptation.
 
1. Value
 
JSON: ValueIs the most basic and important class in jsoncpp. It is used to represent various types of objects. The object types supported by jsoncpp can be seen in JSON: valuetype enumerated values.
 
The JSON: Value class can be used as follows:
 
JSON: Value json_temp;//Temporary objectCodeUse
 
Json_temp ["name"] = JSON: Value ("huchao ");
 
Json_temp ["Age"] = JSON: Value (26 );
 
JSON: Value root;//Indicates the entire JSON object.
Root ["key_string"] = JSON: Value ("value_string ");//Create a new key (Name: key_string) and assign the string value "value_string ".
 
Root [& quot; key_number & quot;] = JSON: Value (12345 );//Create a new key (named: key_number) with the value 12345.
 
Root ["key_boolean"] = JSON: Value (false );//Create a new key (named: key_boolean) with the bool value: false.
 
Root [& quot; key_double & quot;] = JSON: Value (12.345 );//Create a new key (named: key_double) with the double value 12.345.
Root ["key_object"] = json_temp;//Create a key (named: key_object) and assign the JSON: Value Object value.
 
Root ["key_array"]. append ("array_string ");//Create a new key (named: key_array) with the array type. assign a value to the first element as a string: "array_string ".
 
Root [& quot; key_array & quot;]. append (1234 );//Assign a value to the array key_array and assign a value to the second element: 1234.
 
JSON: valuetype type = root. Type ();//Obtain the root type. The objectvalue type is used here.
Note: Unlike C ++, JavaScript Arrays can be of any type, so jsoncpp can also be used.
 
The above usage can already meet the needs of most JSON applications. Of course, jsoncpp also has some other features, such as setting comments, comparing JSON sizes, and exchanging JSON objects, which are easy to use, try it by yourself.
 
2. Writer
 
 
 
As mentioned above, the usage of JSON: value is now time to view the content of the assignment. view the JSON content and use the writer class.
 
Jsoncpp JSON: The writer class is a pure virtual class and cannot be used directly. Here we use the subclass of JSON: writer: JSON: fastwriter, JSON: styledwriter, JSON: styledstreamwriter.
 
As the name suggests, using JSON: fastwriter to process JSON should be the fastest. Let's try it.
 
JSON: fastwriter fast_writer;
 
STD: cout <fast_writer.write (Root) <STD: Endl;
 
 
 
Output result:
 
{"Key_array": ["array_string", 1234], "key_boolean": false, "key_double": 12.3450, "key_number": 12345, "key_object": {"Age ": 26, "name": "huchao"}, "key_string": "value_string "}
Again, as the name implies, styledwriter is formatted JSON. Let's take a look at JSON: How styledwriter is formatted.
 
JSON: styledwriter styled_writer;
 
STD: cout <styled_writer.write (Root) <STD: Endl;
 
Output result:
 
{"Key_array": ["array_string", 1234], "key_boolean": false, "key_double": 12.3450, "key_number": 12345, "key_object": {"Age ": 26,"Name": "huchao"}, "Key_string": "value_string "}
 
3. Reader
 
JSON: ReaderIt is used for reading. It is used to convert a string to a JSON: Value Object. Here is a simple example.
 
JSON: reader;
 
JSON: Value json_object;
 
Const char * json_document = "{\" Age \ ": 26, \" Name \ ": \" huchao \"}";
If (! Reader. parse (json_document, json_object ))
 
Return 0;
 
STD: cout <json_object ["name"] <STD: Endl;
 
STD: cout <json_object ["Age"] <STD: Endl;
 
 
 
Output result:
 
"Huchao"
 
26
 
 
 
It can be seen that the above Code has Parsed the JSON string.
 
--------------------------------------
 
[1]: The simplest way to use third-party source code is to directly add files to the project, but this is not conducive to source code and Software Product Management. It is not recommended for general software development.
 
[2]: If you really need to compile it into a dynamic link library or a static import/export database, you can use vs to create a project attribute and then set it in project --> properties.
 
Address: http://hi.baidu.com/awz_tiger/blog/item/d165970b4ca967fc36d122a4.html
 another useful address is: Good http://blog.csdn.net/vagrxie/article/details/5754179 

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.