Introduction to JSON usage

Source: Internet
Author: User

"Introduction to JSON "

Jsoncpp mainly contains three types of Class:value, Reader, Writer. All objects in Jsoncpp, class names in namespace Json, contain json.h.

"VALUE"

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

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

Json::value json_temp; Temporary object 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"); Create a new Key (named: key_string) and give the string value: "Value_string".

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

root["Key_boolean"] = Json::value (false); Create a new Key (named: Key_boolean) and give the bool value: false.

root["key_double"] = Json::value (12.345); Create a new Key (named: key_double) and give the Double value: 12.345.

root["Key_object"] = json_temp; Create a new Key (named: key_object) to give the Json::value object value.

root["Key_array"].append ("array_string"); Create a new Key (named: key_array), type an array, and assign a value to the first element as a string: "Array_string".

root["Key_array"].append (1234); An array of key_array assignments, with the second element assigned a value of: 1234.

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

"WRITER"

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 implies, using Json::fastwriter to deal with Json should be the fastest, let's try.

Json::fastwriter Fast_writer;

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

The output is:

{"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 implies, with Json::styledwriter is formatted Json, below we see how json::styledwriter is formatted.

Json::styledwriter Styled_writer;

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

The output is:

{

"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"

}

"VALUE"

Json::reader is used to read, say the exact point, is used to convert a string to a Json::value object.

Let's look at 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;

The output is:

"Huchao"

26

From:http://www.cnblogs.com/tekkaman/archive/2011/10/14/2211404.html

Introduction to JSON usage

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.