C++ query of JSON subObject error -
C++ query of JSON subObject error -
my code compiles without error @ runtime next error ->
testgetprice2.o: rapidjson/include/rapidjson/document.h:1125: rapidjson::sizetype rapidjson::genericvalue<encoding, allocator>::size() const [with encoding = rapidjson::utf8<>; allocator = rapidjson::memorypoolallocator<>; rapidjson::sizetype = unsigned int]: assertion `isarray()' failed.
where failing in () loop
std::cout << document[i]["lasttradeprice"].
i trying query subobject "lasttradeprice" accessing object incorrectly.
how query subobject correctly/get rid of error?
code ->
#include "rapidjson/include/rapidjson/document.h" #include <iostream> #include <fstream> #include <stdio.h> #include <curl/curl.h> #include <unistd.h> #include <unordered_map> #include <string> using namespace rapidjson; struct mydata { std::fstream *file; std::string *str; }; size_t write_data(void *ptr, size_t size, size_t nmemb, mydata *data) { size_t numbytes = size * nmemb; if (data->file) data->file->write((char*)ptr, numbytes); if (data->str) *data->str += std::string((char*)ptr, numbytes); homecoming numbytes; } //function coin info , perform analysis int getdata() { int count = 0; //begin non terminating loop while(true) { count++; curl *curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, curlopt_url, "http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=155"); std::fstream file("/home/coinz/cryptsy/myfile.txt", std::ios_base::out | std::ios_base::ate); std::string json; mydata data; data.file = &file; data.str = &json; curl_easy_setopt(curl, curlopt_writefunction, &write_data); curl_easy_setopt(curl, curlopt_writedata, &data); /* perform request, res homecoming code */ curlcode res = curl_easy_perform(curl); /* check errors */ if (res != curle_ok) { std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl; } else { file << std::endl; //begin deserialization document document; document.parse(json.c_str()); (sizetype = 0; < document.size(); i++){ std::cout << document[i]["lasttradeprice"].getstring() << std::endl; } //assert(document.hasmember("return")); //assert(document["return"].isstring()); //std::cout << "the lastly traded cost = " << document["return"].getstring() << std::endl; } /* cleanup */ curl_easy_cleanup(curl); } //timer url request. *adujst me desired* usleep(10000000); } homecoming 0; } //le main int main(void) { getdata(); }
gdb ->
(gdb) run starting program: /home/coinz/cryptsy/testgetprice2.o warning: not load shared library symbols linux-vdso.so.1. need "set solib-search-path" or "set sysroot"? [thread debugging using libthread_db enabled] using host libthread_db library "/lib64/libthread_db.so.1". [new thread 0x7ffff5cc9700 (lwp 24708)] [thread 0x7ffff5cc9700 (lwp 24708) exited] testgetprice2.o: rapidjson/include/rapidjson/document.h:1125: rapidjson::sizetype rapidjson::genericvalue<encoding, allocator>::size() const [with encoding = rapidjson::utf8<>; allocator = rapidjson::memorypoolallocator<>; rapidjson::sizetype = unsigned int]: assertion `isarray()' failed. programme received signal sigabrt, aborted. 0x00007ffff6fec795 in raise () /lib64/libc.so.6
according original json construction following:
{ "success":1, "return": { "markets": { "drk": { "marketid":"155", "label":"drk\/btc", "lasttradeprice":"0.00553336", "volume":"14615.30200941", "lasttradetime":"2014-10-25 14:35:09", "primaryname":"darkcoin", "primarycode":"drk", "secondaryname":"bitcoin", "secondarycode":"btc", "recenttrades":
you tying access
document[i]["lasttradeprice"]
but should access
documents["return"]["markets"][i]["lasttradedprice"]
c++ json deserialization
Comments
Post a Comment