) Use C ++ to process JSON data exchange formats

Source: Internet
Author: User

Conversion from: http://blog.csdn.net/xt_xiaotian/article/details/5648388 (reproduced when the original modified a case of errors)

Also, download http://sourceforge.net/projects/jsoncpp

My mint13 system, compile jsoncpp-src-0.5.0:
$ Jsoncpp-src-0.5.0 CD
$ Sudo apt-Get install scons
$ Scons platform = Linux-gcc
$ Sudo CP libs/linux-gcc-4.6/*/usr/local/lib/
$ Sudo CP-r include/JSON/usr/local/include/
Note: In README, it seems that there is a bug in the dynamic library... not sure. I use the static library anyway.

UseC ++ProcessingJSONData Exchange Format

 

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.

 

Ii. Processing of this ArticleJSONOfC ++Library

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.

 

III,JsoncppInWindowsCompile

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,JsoncppUsage Details

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.ProgramIt is unicode encoded. 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:CodeUse

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;

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.

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.