VC ++ parses JSON strings

Source: Internet
Author: User
I. Summary JSON is called JavaScript Object Notation. JSON is used to mark JavaScript objects as its name suggests. 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. 2. This article selects the C ++ library for processing JSON. This article selects 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. For: http://sourceforge.net/projects/jsoncpp (the jsoncpp version used in this article is: 0.5.0) III. jsoncpp in Windows compilation to use third-party source code library, the first step is to compile, compile the source code file into a convenient dynamic link library, static link library, or static import library. 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 a static import library, so we choose to use a static Link Library. Jsoncpp has been well processed, and all compilation options have been configured. You can start compilation by enabling makefiles/vs71/jsoncpp. sln. Iv. Usage Details of jsoncpp 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: value can only process strings of the ANSI Type. If the C ++ program uses unicode encoding, it is best to add an adapt class for adaptation. 1. Value JSON: value is 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. JSON: Value class: JSON: Value json_temp; // a temporary object for the following code: json_temp ["name"] = JSON :: value ("huchao"); json_temp ["Age"] = JSON: Value (26); JSON: Value root; // represents the entire JSON object root ["key_string"] = JSON: Value ("value_string"); // creates a key (named: key_string) and assigns the string value: "value_string ". Root ["key_number"] = JSON: Value (12345); // create a key (named: key_number) with a value of 12345. Root ["key_boolean"] = JSON: Value (false); // create a key (named: key_boolean) and assign the bool value: false. Root ["key_double"] = JSON: Value (12.345); // create a key (named: key_double) and assign 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 key (named: key_array). The type is an array. assign a value to the first element as a string: "array_string ". Root ["key_array"]. 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 describes how to use JSON: value as above. Now it is time to view the content of the value 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 suggests, styledwriter is the formatted JSON. Let's take a look at JSON: How is styledwriter 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: reader is used to read data. 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; the output result is: "huchao" 26. The preceding Code has Parsed the JSON string.

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.