Wednesday 15 June 2011

json conversion error c++ IsString failed -



json conversion error c++ IsString failed -

i have info in json object can't seem to.

error:

testgetprice3.o: testgetprice3.cpp:71: int getdata(): assertion `document["success"].isstring()' failed.

i have tried switching datatypes, errors. how correctly access json object data?

#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()); assert(document.hasmember("success")); assert(document["success"].isstring()); //std::cout << "the lastly traded cost = " << document["lasttradeprice"].getstring() << std::endl; } /* cleanup */ curl_easy_cleanup(curl); } //timer url request. *adujst me desired* usleep(10000000); } homecoming 0; } //le main int main(void) { getdata(); }

looking @ json data, success fellow member int (as not surrounded quote marks).

more info on json types can found here: http://en.wikipedia.org/wiki/json#data_types.2c_syntax_and_example

if success fellow member string, json info this: {"success":"1",...

therefore when phone call assert(document["success"].isstring()) causes error, success fellow member not string, int.

since success fellow member int, alter assertion test whether it's int - assert(document["success"].isint())

c++ json deserialization libcurl

No comments:

Post a Comment