Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to access the second column directly or iteratively.

```[
{
"Player_no": 2,
"player_name": "Warner",
"team": "Aus",
"age": 33
},
{
"Player_no": 3,
"player_name": "virat",
"team": "India",
"age": 30
},
{
"Player_no": 4,
"player_name": "smith",
"team": "SA",
"age": 30
}
]```

What I have tried:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/ostreamwrapper.h>

int main()
{
    using namespace rapidjson;

    std::ifstream ifs { R"(C:\Test\Test.json)" };
    if ( !ifs.is_open() )
    {
        std::cerr << "Could not open file for reading!\n";
        return EXIT_FAILURE;
    }

    IStreamWrapper isw { ifs };

    Document doc {};
    doc.ParseStream( isw );

    StringBuffer buffer {};
    Writer<StringBuffer> writer { buffer };
    doc.Accept( writer );

    if ( doc.HasParseError() )
    {
        std::cout << "Error  : " << doc.GetParseError()  << '\n'
                  << "Offset : " << doc.GetErrorOffset() << '\n';
        return EXIT_FAILURE;
    }

    const std::string jsonStr { buffer.GetString() };

    std::cout << jsonStr << '\n';

    doc[ "Player_no" ] = "17";

    std::ofstream ofs { R"(C:\Test\NewTest.json)" };
    if ( !ofs.is_open() )
    {
        std::cerr << "Could not open file for writing!\n";
        return EXIT_FAILURE;
    }

    OStreamWrapper osw { ofs };
    Writer<OStreamWrapper> writer2 { osw };
    doc.Accept( writer2 );

    return EXIT_SUCCESS;
}
Posted
Updated 28-Oct-22 0:45am

1 solution

NOt familiar with that lib, so I did a quick Google Search: rapidjson example - Google Search[^] and found this: RapidJSON: Tutorial[^]
 
Share this answer
 
Comments
Abishek Samuel 28-Oct-22 6:49am    
Thank you but it is hard to find the required solution in that documentation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900