Jsoncpp parses and assembles Arrays

Source: Internet
Author: User

Int main ()

{

/*

Array creation and analysis:

Example 1:

String strvalue = "{\" LDH \ ": \" 001 \ ", \" GFC \ ": \" 002 \ ", \" yyj \": \ "003 \", \ "Andy \": [\ "005 \", \ "123 \", \ "True \"]} ";

JSON: reader read;

JSON: Value value;

Value ["LDH"] = "001 ";

Value ["GFC"] = "002 ";

Value ["Andy"]. append ("005 ");

Value ["Andy"]. append ("123 ");

Value ["Andy"]. append ("true ");

// If (read. parse (strvalue, value ))

{

JSON: Value val_array = value ["Andy"];

Int isize = val_array.size ();

For (INT nindex = 0; nindex <isize; ++ nindex)

{

Cout <val_array [nindex] <Endl;

}

}

*/

/*

Example 2:

JSON: reader read;

JSON: Value value;

Value ["LDH"] = "001 ";

Value ["GFC"] = "002 ";

Value item;

Value array;

Item ["andy1"] = "005 ";

Array. append (item );

Item ["andy1"] = "123 ";

Array. append (item );

Item ["andy1"] = "true ";

Array. append (item );

Value ["Andy"] = array;

Cout <value. tostyledstring () <Endl;

JSON: Value val_array = value ["Andy"];

Int isize = val_array.size ();

For (INT nindex = 0; nindex <isize; ++ nindex)

{

Cout <val_array [nindex] <Endl;

If (! Val_array [nindex] ["andy1"]. isnull ())

{

Cout <val_array [nindex] ["andy1"] <Endl;

}

}

*/

/*

Example 3:

STD: String strvalue = "{\" Name \ ": \" JSON \ ", \" array \ ": [{\" CPP \": \ "jsoncpp \" },{ \ "Java \": \ "jsoninjava \" },{ \ "php \": \ "support \"}]} ";

JSON: Value value;

Reader read;

If (! Read. parse (strvalue, value ))

{

Return-1;

}

Cout <value. tostyledstring () <Endl;

JSON: Value val_array = value ["array"];

Int isize = val_array.size ();

For (INT nindex = 0; nindex <isize; ++ nindex)

{

Cout <val_array [nindex] <Endl;

If (val_array [nindex]. ismember ("CPP "))

{

Cout <val_array [nindex] ["CPP"] <Endl;

}

}

*/

Getchar ();

Return 0;

}

It was awkward to use jsoncpp at first. For example, there are three arrays in the third example, but val_array [nindex] cannot be used directly. asstring. the format is indeed not clear.

To sum up, no matter which form of array, you can directly look at the content of its corresponding index during parsing, and then we can export its resolution method.

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.