Use C ++ jsoncpp (including source code download)

Source: Internet
Author: User
Preface

JSON is a lightweight data definition format. It is easier to learn and use than XML, and its extended functions are no worse than XML. Using JSON for data exchange is a good choice.
JSON is called JavaScript Object Notation. As the name suggests, JSON is used to mark JavaScript objects. For details, see http://www.json.org /.
This article chooses a third-party library jsoncpp to parse JSON. jsoncpp is a well-known C ++ Parsing Library, which is also the first on the JSON official website.

Introduction to jsoncpp

Jsoncpp mainly contains three types of Class: Value reader writer.
All objects and class names in jsoncpp are in namespace JSON, including JSON. h.
Note: JSON: value can only process ANSI strings.ProgramIt is recommended to add an adapt class to adapt to unicode encoding.

Download and compile

The running environment is: RedHat 5.5 + G ++ version 4.6.1 + GNU make 3.81 + jsoncpp-0.5.0

Yes: http://sourceforge.net/projects/jsoncpp/

Decompress the package to get the jsoncpp-src-0.5.0 folder, we only need the header file and CPP file of jsoncpp, where the header file of jsonscpp is located in jsoncpp-src-0.5.0 \ include \ JSON, And the CPP file of jsoncpp is located in jsoncpp-src-0.5.0 \ SRC \ lib_json.

Here I will list our working directories:

Jsoncpp/ //  Working directory  
| -- Include // Header file root directory
| -- JSON // JSON header file, corresponding to jsoncpp-src-0.5.0 \ include \ JSON
| -- SRC // CPP source code file root directory
| -- JSON // Jsoncpp source code file, corresponding to jsoncpp-src-0.5.0 \ SRC \ lib_json
| -- Main. cpp // Example of calling jsoncpp for our main functionCode

| -- Makefile // Makefile. You don't need to talk about it. If you don't understand it, please refer to my blog's makefile best practices.

 

Deserialization of JSON objects

Suppose there is a JSON object as follows:

{
" Name " : " JSON ″,
" Array " :[
{
" CPP " : " Jsoncpp "
},
{
" Java " :" Jsoninjava "
},
{
" PHP " : " Support "
}
]
}

The anti-serial number code for JSON is as follows:

 Void Readjson (){
Using Namespace STD;
STD :: String Strvalue = " {\ "Name \": \ "JSON \", \ "array \": [{\ "CPP \": \ "jsoncpp \"}, {\ "Java \": \ "jsoninjava \" },{ \ "php \": \ "support \"}]} " ;

JSON: reader;
JSON: Value value;

If (Reader. parse (strvalue, value ))
{
STD :: String Out = Value [ " Name " ]. Asstring ();
STD: cout < Out <STD: Endl;
Const JSON: Value arrayobj = value [ " Array " ];
For (Unsigned Int I = 0 ; I <arrayobj. Size (); I ++)
{
If (! Arrayobj [I]. ismember ( " CPP " ))
Continue ;
Out = Arrayobj [I] [ " CPP " ]. Asstring ();
STD: cout < Out ;
If (I! = (Arrayobj. Size ()- 1 ))
STD: cout <STD: Endl;
}
}
}

 

Serialize a json object
 Void Writejson (){
Using Namespace STD;

JSON: Value root;
JSON: Value arrayobj;
JSON: value item;

Item [ " CPP " ] = " Jsoncpp " ;
Item [ " Java " ] = " Jsoninjava " ;
Item [ " PHP " ] = " Support " ;
Arrayobj. append (item );

Root [" Name " ] = " JSON " ;
Root [ " Array " ] = Arrayobj;

Root. tostyledstring ();
STD :: String Out = Root. tostyledstring ();
STD: cout < Out <STD: Endl;
}

 

Download complete code

Click here to download

After the download, run the following command:

 
Unzip jsoncpp.zip
CD jsoncpp
Make
./Main

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.