Jsoncpp simple example

Source: Internet
Author: User

1. Compile jsoncpp
Extract the source code package from (http://jsoncpp.sourceforge.net/unzip jsoncpp-src-0.5.0.tar.gz "and run it in the directory after its unzipping
$ Scons platform = Linux-gcc
The compiled library file has linux-gcc-4.4.2 and libjson_linux-gcc-4.4.2_libmt.so under its libs/libjson_linux-gcc-4.4.2_libmt.a directory. The header file is included in the extract directory. Install jsoncpp under $ home/usr/jsoncpp.

$ Mkdir ~ /Usr/jsoncpp

$ CP-r include ~ /Usr/jsoncpp

$ CP-r libs ~ /Usr/jsoncpp

2. Simple jsoncpp instance

1) deserialization of JSON objects
For example, the string sequence of a json object is as follows, where "array": [...] indicates the array in the JSON object:

{"Key1": "value1", "array": [{"key2": "value2" },{ "key2": "value3" },{ "key2 ": "value4"}]}

So how can we get the values of key1 and key2 respectively,CodeAs follows:

# Include <iostream> # include <string> # include "JSON/JSON. H "int main (void) {STD: String strvalue =" {\ "key1 \": \ "value1 \", \ "array \": [{\ "key2 \": \ "value2 \" },{ \ "key2 \": \ "value3 \" },{ \ "key2 \": \ "value4 \"}]} "; JSON: reader; JSON: Value value; If (reader. parse (strvalue, value) {STD: String out = value ["key1"]. asstring (); STD: cout <out <STD: Endl; const JSON: Value arrayobj = value ["array"]; (INT I = 0; I <arrayobj. size (); I ++) {out = arrayobj [I] ["key2"]. asstring (); STD: cout <out; if (I! = Arrayobj. Size ()-1) STD: cout <STD: Endl;} return 0 ;}

Compile the connection
$ G ++ jscpp1.cpp-I $ home/usr/jsoncpp/include /~ /Usr/jsoncpp/libs/linux-gcc-4.5.2/libjson_linux-gcc-4.5.2_libmt.a
$./A. Out
Value1
Value2
Value3
Value4

2) serialize a json object
Create a JSON object that contains arrays and serialize the JSON object into a string. The Code is as follows:

# Include <iostream> # include <string> # include "JSON/JSON. H "int main (void) {JSON: Value root; JSON: Value arrayobj; JSON: value item; For (INT I = 0; I <10; I ++) {item ["key"] = I; arrayobj. append (item);} root ["key1"] = "value1"; root ["key2"] = "value2"; root ["array"] = arrayobj; // root. tostyledstring (); STD: String out = root. tostyledstring (); STD: cout <out <STD: Endl; return 0 ;}

Compile the connection
$ G ++ jscpp2.cpp-I $ home/usr/jsoncpp/include /~ /Usr/jsoncpp/libs/linux-gcc-4.5.2/libjson_linux-gcc-4.5.2_libmt.a
$./A. Out
{
"Array ":[
{
"Key": 0
},
{
"Key": 1
},
{
"Key": 2
},
{
"Key": 3
},
{
"Key": 4
},
{
"Key": 5
},
{
"Key": 6
},
{
"Key": 7
},
{
"Key": 8
},
{
"Key": 9
}
],
"Key1": "value1 ",
"Key2": "value2"
}

Note: The above two codes are from the Internet, and these two instances are good, so they are borrowed here.

(Indicate the author and source when reprinting. Do not use it for commercial purposes without permission)
MoreArticleVisit my blog: http://www.cnblogs.com/logicbaby

    1. compile jsoncpp

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.