The Json method is used in C ++.

Source: Internet
Author: User

The Json method is used in C ++.

Use C ++ to process JSON data exchange formats

 

I. Summary

JSON is called JavaScript Object Notation. JSON 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.

 

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

Jsoncpp mainly contains 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 objects of various types. 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 object for use by the following code:

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 Key (named: key_string) and assign 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

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: 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 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.


In java, how does one use json to receive a list?

List can be directly transmitted to the foreground, and can be obtained directly using the c tag <c: forEach>, but the related get set Method of the list needs to be written in the action.

Which of the following is a practical example of json?

JSON, the full name is JavaScript Object Notation. It is a lightweight data exchange format based on the JavaScript programming language ECMA-262 3rd Edition-December 1999 standard, mainly used to exchange data with the server. Similar to XML, it is independent of languages and has great advantages in cross-platform data transmission.
The above is an encyclopedia explanation, which can explain the meaning and function of json. json is actually a key-value pair for storing data. For example:
Var a = {"one": "one", "two": "two", "three": "three"}, a is a json,. the value of one is one, and an array can be embedded.
Var B = {"A": "[1, 3, 4, 5, 6, 7]", "B": "[a, B, c, d, e]"}, b. the value of A [0] is 1. json variables can also be embedded with json, and arrays can be embedded. The value method is the same as above. The array uses subscript and json to take the name of the key-value pair, easy to use, simple data storage format

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.