1

I'm trying to read a JSON file using RapidJson in C++ and am running into a problem

Here is the JSON file below.

{
    "organizations":[
        
    ],
    "uuid":"e68495a399ffde815e06101d8ff0c8b69f205b9d",
    "thread":{
        "social":{
            "gplus":{
                "shares":0
            },
            "pinterest":{
                "shares":0
            },
            "vk":{
                "shares":0
            },
            "linkedin":{
                "shares":0
            },
            "facebook":{
                "likes":0,
                "shares":0,
                "comments":0
            },
            "stumbledupon":{
                "shares":0
            }
        },
        "site_full":"www.wsj.com",
        "main_image":"https://si.wsj.net/public/resources/images/BN-XA479_WATCH__SOC_20180116213409.jpg",
        "site_section":"http://www.wsj.com/xml/rss/3_7201.xml",
        "section_title":"WSJ.com: Lifestyle",
        "url":"https://www.wsj.com/articles/the-worlds-most-irritating-watch-only-42-200-1516209909",
        "country":"US",
        "domain_rank":387,
        "title":"The World’s Most Irritating Watch? Only $42,200! - WSJ",
        "performance_score":0,
        "site":"wsj.com",
        "participants_count":1,
        "title_full":"",
        "spam_score":0.0,
        "site_type":"news",
        "published":"2018-01-17T19:25:00.000+02:00",
        "replies_count":0,
        "uuid":"e68495a399ffde815e06101d8ff0c8b69f205b9d"
    },
    "author":"Michael Clerizo",
    "url":"https://www.wsj.com/articles/the-worlds-most-irritating-watch-only-42-200-1516209909",
    "ord_in_thread":0,
    "title":"The World’s Most Irritating Watch? Only $42,200! - WSJ",
    "locations":[
        
    ],
    "entities":{
        "persons":[
            {
                "name":"breguet",
                "sentiment":"none"
            },
            {
                "name":"philippe",
                "sentiment":"none"
            },
            {
                "name":"jones",
                "sentiment":"none"
            },
            {
                "name":"vacheron constantin",
                "sentiment":"none"
            },
            {
                "name":"crispin jones",
                "sentiment":"none"
            }
        ],
        "locations":[
            
        ],
        "organizations":[
            {
                "name":"wsj",
                "sentiment":"negative"
            },
            {
                "name":"audemars piguet",
                "sentiment":"none"
            }
        ]
    },
    "highlightText":"",
    "language":"english",
    "persons":[
        
    ],
    "text":"0 COMMENTS Q: Lately, I’m seeing a lot of watches with odd designs that don’t seem to tell time clearly. Is this some weird fad? \nA: Watchmaking is about precision, but it’s also about creativity and, in their nerdy pursuit of beauty , watch designers don’t always take practicality into account. Traditional watchmakers such as Breguet, Patek Philippe and Rolex generally adhere to standards when it comes to the hands and numbers on a dial, sticking to design codes developed over decades or centuries. Other brands, like Vacheron Constantin and Audemars Piguet, are more likely to rebel a little. \nBut some intriguing new watch designers are chucking all the rules out of the Swiss window, replacing classic numerals with decidedly less user-friendly elements such as floating discs and skulls. I talked to three relative newbies in the watchmaking world about the conceptual possibilities beyond a standard watch dial. They certainly don’t see their work as faddish. \nCrispin Jones of London-based Mr Jones Watches has produced the Last Laugh, a watch whose dial depicts a skull. To decipher the time, you must drop everything and peer closely at the skull’s teeth. Hours appear on two front teeth of the upper row while the lower row shows the minutes.\nMr. Jones considers the timepiece a meditation on mortality. As for time-telling conventions, he said, “I find it really interesting that we accept that a short line indicates hours and a longer line indicates minutes. We don’t question this—it seems to be a part of the natural order of things, but it’s a design, a human invention.” An invention ripe for reinvention, apparently. \nIsraeli watchmaker Itay Noy’s Part Time Sun and Moon watch is an exercise in experimentation. The enigmatic dial features five windows: a central one showing minutes, surrounded by four additional apertures indicating seconds, hours, day (with a sun) and night (with a moon). Sound complicated? That may be the point: Mr. Noy believes his design increases the wearer’s sense of involvement with his watch—which might not sound that desirable, unless you’re Tom Hanks’s “Castaway” character and are sick of talking to your soccer ball. \nMr. Noy said of his experiment, “I wanted to break the ordinary look of a watch by moving the hands from the center and adding more information. The wearer needs to collect pieces of information from five windows in order to read the time.” Ideally, the wearer is not rushing to catch a plane.\n‘ You probably wouldn’t consider going for a wildly experimental watch unless you already own a more standard one. ’ \nAntwerp industrial designer Benoît Mintiens of Ressence designed train interiors and luggage before he transitioned into watches, and says he believes the essence of any good product is functionality. Yet how functional his creative watch dials are might depend on how thoroughly you have studied the detailed instructions that come with his watches. A Ressence watch features a grouping of discs floating on oil. The symbol on the hour disc serves as the hand. Other discs indicating seconds, minutes, days of the week, temperature of the oil and status of the entire mechanism float around like tropical fish in an aquarium. It’s a trippy user experience, to say the least. \nYou probably wouldn’t consider going for a wildly experimental watch unless you already own a more standard one . But for the collector looking to branch out, or even someone who’s just mad about ingenious design, it’s worth exploring these less than forthcoming faces—even if it means that telling time might become rather time-consuming.\nMore in Style & Fashion \nCritic’s Choice: The 8 Best Watches of 2017 December 18, 2017 Travel Watches: How Savvy Frequent Fliers Stay on Schedule November 20, 2017",
    "external_links":[
        
    ],
    "published":"2018-01-17T19:25:00.000+02:00",
    "crawled":"2018-01-17T19:45:40.080+02:00",
    "highlightTitle":""
}

I am trying to read the entities and organizations sub part of the json file. I have tried the code below.

    ifstream streamy(filenames[i]); //open the 'ith' file;
    //filenames is a vector that stores each if the file paths (already confirmed it works). 

    while (getline(streamy, tempFileContents)) //reading the contents of file and put in string.
    {
        fileContents += tempFileContents;
    }

    //cout<<fileContents<<endl;
    streamy.close();

    doc.Parse(fileContents.c_str());
    // DOM to string
    StringBuffer buffer;
    Writer<StringBuffer> writer(buffer);

    doc.Accept(writer);

    // Print the output
    //std::cout << buffer.GetString() << std::endl; //this prints out the whole file
    cout<<thefilename<<endl;
    doc["entities"]["organizations"].IsObject();
    doc["entities"]["organizations"].IsArray();

    for (auto &v : doc["entities"]["organizations"].GetArray ())
    {
        cout << "Value: " <<  v.GetString() << endl;
    }

The error: Assertion failed: (IsString()), function GetStringLength, file document.h, line 1832.

Line 1832 of RapidJson document.h file: const Ch* GetString() const { RAPIDJSON_ASSERT(IsString()); return DataString(data_); }

Shouldn't this print:

{
    "name":"wsj",
    "sentiment":"negative"
},
{
    "name":"audemars piguet",
    "sentiment":"none"
}
rturrado
  • 7,699
  • 6
  • 42
  • 62
Skydaz
  • 41
  • 2
  • I'd say `doc["entities"]["organizations"].GetArray()` should return an array of maps, each of which you are accessing through `v`; the error seems to say that you cannot call `GetString()` on `v`; what you could probably do is to loop every map of the array `v` and print the values of the keys `name` and `sentiment` for each of them: `for (const auto& m : v) { std::cout << "name=" << m["name"] << ", "; std::cout << "sentiment" = m["sentiment"] << "\n"; }`. – rturrado Apr 26 '22 at 21:56

0 Answers0