JSON 4 C + +

Source: Internet
Author: User




Turn from: http://hi.baidu.com/awz_tiger/item/e877551d9ac0e1f665eabfd9

The original source can not be tested, in this heartfelt thanks to the author of the article.


Using C + + to process JSON data interchange formats

I. Summary

The full name of JSON is: JavaScript Object notation, as the name implies, JSON is used to mark JavaScript objects, and JSON is officially interpreted as: JSON is a lightweight data transmission format.

This article does not detail the details of JSON itself, and is designed to discuss how to handle JSON using the C + + language. For more specific information about JSON, see the JSON website: http://www.json.org.

Second, this article chooses the C + + library that processes JSON

This article selects a third-party library jsoncpp to parse JSON. Jsoncpp is the more famous C + + JSON parsing library. In the JSON official website is also the most.

The download address is: Http://sourceforge.net/projects/jsoncpp. The jsoncpp version used in this article is: 0.5.0.

Iii. jsoncpp Compilation under Windows

To use the Third-party Source Library, the first step is to compile, the source file compiled into our convenient use of dynamic link library, static link library or static import library [1].

Jsconcpp the source files for JSON parsing are distributed under Include/json, Src/lib_json. In fact, Jsoncpp source code is not much, in order to facilitate product management, there is no need to compile it as a dynamic link library or static import library, so we chose to use the static link library [2].

Jsoncpp has been handled very well, all the compilation options have been configured, open Makefiles/vs71/jsoncpp.sln can begin to compile (the default is to use the VS2003 compiler, when open directly according to the VS2005 prompt conversion).

Iv. Jsoncpp Use of detailed

Jsoncpp mainly includes three kinds of class:value, Reader, Writer. All objects in Jsoncpp, class names in namespace json, contain json.h.

Json::value can only handle ANSI-type strings, and if C + + programs are encoded in Unicode, it is best to add a adapt class to fit.

1, Value

Json::value is the most basic and important class in Jsoncpp, and is used to represent various types of objects, Json::valuetype enumeration values are visible for Jsoncpp supported object types.

You can use the Json::value class as follows:

Json::value json_temp; Temporary objects for use with 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 new key (named: key_string), giving the string value: "Value_string".

root["Key_number"] = Json::value (12345); Create a new key (named: Key_number), give the value: 12345.

root["Key_boolean"] = Json::value (false); Creates a new key (named: Key_boolean), giving the bool value: false.

root["key_double"] = Json::value (12.345); Creates a new key (named: key_double), giving the double value: 12.345.

root["Key_object"] = json_temp; Creates a new key (named: Key_object), giving Json::value object values.

root["Key_array"].append ("array_string"); Creates a new key (named: Key_array), the type is an array, and the first element is assigned a string: "Array_string".

root["Key_array"].append (1234); Assigns a value to an array, and assigns a value of 1234 to the second element: Key_array.

Json::valuetype type = Root.type (); Gets the type of root, which is the Objectvalue type.

Note: Unlike C + +, JavaScript arrays can be any type of value, so jsoncpp can also be.

As the previous usage has already met most of the JSON apps, of course Jsoncpp has some other things to do, such as setting annotations, comparing JSON sizes, swapping JSON objects, and so on, which are easy to use.

2, Writer

As mentioned in the Json::value, now it's time to look at the contents of the current assignment, look at the JSON content, use the writer class.

The Json::writer class of Jsoncpp is a pure virtual class and cannot be used directly. Here we use Json::writer subclasses: 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;

The output results are:

{"Key_array": ["array_string", 1234], "Key_boolean": false, "key_double": 12.3450, "Key_number": 12345, "Key_object": {" Age ":", "name": "Huchao"}, "key_string": "Value_string"}

Again, as the name suggests, using Json::styledwriter is formatted JSON, let's look at how the Json::styledwriter is formatted.

Json::styledwriter Styled_writer;

Std::cout << styled_writer.write (root) << Std::endl;

The output results are:

{

"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 for reading, the exact point is used to convert a string to a Json::value object, let's look at a simple example here.

Json::reader 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 results are:

"Huchao"

26

Visible, the above code has parsed the JSON string.

--------------------------------------

[1]: The simplest way to use the third party source code is directly to add the file to the project, but this is not conducive to the source code, software Product management, for general software development, do not recommend use.

[2]: If you really need to compile into a dynamic link library, static import library, you can use VS to create a new project properties, and then in the project--> properties in the appropriate settings.

Related Article

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.