Tencent / rapidjson

A fast JSON parser/generator for C++ with both SAX/DOM style API
http://rapidjson.org/
Other
14.17k stars 3.52k forks source link

How to deal with stream of json object? #1090

Closed ShiY93 closed 6 years ago

ShiY93 commented 6 years ago

I have a .josn file, store data like this:

{"asin": "0001048791", "salesRank": {"Books": 6334800}, "imUrl": "http://ecx.images-amazon.com/images/I/51MKP0T4DBL.jpg", "categories": [["Books"]], "title": "The Crucible: Performed by Stuart Pankin, Jerome Dempsey & Cast"} {"asin": "0001048236", "categories": [["Books"]], "description": ""One thing is certain, Sherlockians, put aside your Baring-GouldAnnotated, your Folio So cietyIllustrated-for the time being, the Oxford is the edition to curl up with on a winter"s night"--The Chicago Tribune"An incomparable gift book; or , should you find it impossible to surrender up such treasures, the best of gifts to oneself"--USA Today"To the true Sherlockian, this will be a treas ure; to otherwise diverted detective story fans, it is a rich lode for discovery"--Denver Post"The complete and authentic adventures of the legendary detective--expertly edited and annotated by a team of Holmes scholars....in a handsome, boxed set....A lovely gift"--The Christian Science Monitor"Her e in nine volumes...are all the adventures of Holmes and Watson. Each book has an introduction, something new and fascinating for even the most devoted Holmes ians plus a series of intelligent notes at the back of each volume."--Oxford Times"TheOxford Sherlock Holmes, a new edition of the stories, is a splen did piece of publishing. Nine compact volumes, beautifully produced, each with a stimulating introduction; clear type, accurate texts, a handy chronology, a h elpful bibliography. And, most valuable of all, explanatory notes running to 50 pages or more per volume." --John Gross, writing inSunday Telegraph--This text refers to thePaperbackedition.", "title": "The Sherlock Holmes Audio Collec tion", "price": 9.26, "salesRank": {"Books": 8973864}, "imUrl": "http://ecx.images-amazon.com/images/I/51DH145C5JL.jpg", "related": {"also_viewed": ["1442300191", "9626349786", "1602837155", "1598879162", "1400115159", "1478396202", "1408426250", "B007PM2A4A", "1609980603"], "buy_after_viewing": ["0312089457"]}} {"asin": "0000401048", "title": "The rogue of publishers" row;: Confessions of a publisher (A Banner Book)", "imUrl": "http://ecx.images-amazon.com/images/I/41bchvIfgaL.jpg", "related": {"also_viewed": ["068240103X"]}, "salesRank": {"Books": 6448843}, "categories": [["Books"]]} {"asin": "0001019880", "title": "Classic Soul Winner"s New Testament Bible", "price": 5.39, "imUrl": "http://ecx.images-amazon.com/images/I/61LcHUdvS1L.jpg", "related": {"also_viewed": ["B003HMB5FC", "0834004593"], "buy_after_viewing": ["031095360X", "0834004577"]}, "salesRank": {"Books": 9589258}, "categories": [["Books"]]}

I use this method to process .json file

ifstream handle("meta_Books.json"); if(handle.is_open()){ //cout<<"open success"<<endl; const char* json; string strjson; int i=1; while(getline(handle,strjson)){ if(i>4) break; cout<<strjson<<endl; cout<<strjson.length()<<endl; i++; json=strjson.c_str(); StringStream s (json); Document document; document.ParseStream(s); Value::ConstMemberIterator itr = document.FindMember("asin"); cout<name.GetString()<<" = "<< itr->value.GetString()<<endl; cout<<endl; } }

but after I run it , cannot parse the second object,

I can get asin of the first object, however, I get nothing of second object, the console informs me Assertion failed: (IsObject()), function FindMember, need help

pah commented 6 years ago

If you want to parse a JSON file/stream with multiple objects in it, you need to use the kParseStopWhenDoneFlag. Quoting from http://rapidjson.org/md_doc_dom.html#Parsing:

After parsing a complete JSON root from stream, stop further processing the rest of stream. When this flag is used, parser will not generate kParseErrorDocumentRootNotSingular error. Using this flag for parsing multiple JSONs in the same stream.

Use it with:

  document.ParseStream<kParseStopWhenDoneFlag>(s);

and then loop over until the stream reaches the end.