我的代码编译没有错误,但在运行时我收到以下错误 - >
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.
失败的地方是我的for()循环
std::cout << document[i]["lasttradeprice"].
我正在尝试查询subObject&#34; lasttradeprice&#34;但是没有错误地访问该对象。
如何正确查询子对象/摆脱此错误?
代码 - &gt;
#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);
return numBytes;
}
//function to get coin data and 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 the request, res will get the return code */
CURLcode res = curl_easy_perform(curl);
/* Check for 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());
for (SizeType i = 0; i < document.Size(); i++){
std::cout << document[i]["lasttradeprice"].GetString() << std::endl;
}
//assert(document.HasMember("return"));
//assert(document["return"].IsString());
//std::cout << "The Last Traded Price is = " << document["return"].GetString() << std::endl;
}
/* always cleanup */
curl_easy_cleanup(curl);
}
//timer for URL request. *ADUJST ME AS DESIRED*
usleep(10000000);
}
return 0;
}
//Le Main
int main(void)
{
getData();
}
gdb - &gt;
(gdb) run
Starting program: /home/coinz/cryptsy/testGetprice2.o
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you 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.
Program received signal SIGABRT, Aborted.
0x00007ffff6fec795 in raise () from /lib64/libc.so.6
答案 0 :(得分:0)
根据原始JSON,结构如下:
{
"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":
你想要访问
document[i]["lasttradeprice"]
但您应该访问
documents["return"]["markets"][i]["lasttradedprice"]
答案 1 :(得分:0)
用
替换你的循环std :: cout&lt;&lt; &#34;最后交易价格=&#34; &LT;&LT;文件[&#34;返回&#34;] [&#34;市场&#34;] [&#34; DRK&#34;] [&#34; lasttrad eprice&#34;]。GetString()&lt;&lt;的std :: ENDL;