Generate nested JSON in VC

Source: Internet
Author: User

# Include "jsoncpp/include/JSON. H"

// Use the JSON class downloaded from the internet

 

 

JSON: Value root;
JSON: Value var;
JSON: Value jfdata;

 

// For example, a directory level is as follows:

C: // files // testfile // datas // {1. Text, 2. Text, 3. Text. 4. Text}

 

// The generated directory is as follows:

{"Folders": "Files", "data": [{"Folders": "testfile", "data": [{"Folders": "datas ", "data": [{"FILENAME": "1. text "},{" FILENAME ":" 2. text "},{" FILENAME ":" 3. text "},{" FILENAME ":" 4. text "}]}

 

 

Cstringarry folderslist; // assume that all directory structures have been saved.

Folderslist. Add ("Files ");

Folderslist. Add ("ftestfile ");

Folderslist. Add ("datas ");

 

 

Cstringarry fileslist; // assume that all directory structures have been saved.

Fileslist. Add ("1. Text ");

Fileslist. Add ("2. Text ");

Fileslist. Add ("3. Text ");

Fileslist. Add ("4. Text ");

 

// Convert the file into JSON

JSON: Value jldata;

For (INT ifilepos = 0; ifilepos <4, ifilepos ++)

{

JSON: Value piece_ex; // here it store just one piece
// Next 4 lines to apply piece value to JSON struct
Piece_ex ["Folders"] = fileslist. Get (ifilepos );
Jldata. append (piece_ex); // OK, yes we just have apply one piece, then push back to the array

}

 

// Generate the nested JSON Directory, which is generated from the deepest layer.

JSON: Value jcfdata;
For (INT ifolderspos = 2; ifolderspos> = 0; -- ifolderspos)
{
JSON: Value jccdata;
JSON: Value jccroot;
Jccdata ["fname"] = folderslist. Get (ifolderspos );
If (ifolderspos = listfolders. getcount ()-1)
{
Jccdata ["Folders"] = jldata;
}
Else
{
Jccdata ["Folders"] = jcfdata;
}
Jcfdata = jccdata;
}
Jfdata. append (jcfdata );

 

VaR ["data"] = jfdata; // Yes, store pieces in Var [value]
Root. append (VAR );
JSON: fastwriter writer;
Cstring szdata (writer. Write (VAR). c_str ());

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.