Как получить переменную из массива JSON rapidjson
Не могу получить переменную "id" Пример JSON:
{
"response": [{
"id": 1,
"name": "ВКонтакте API",
"screen_name": "apiclub",
"is_closed": 0,
"type": "group",
"is_admin": 1,
"admin_level": 3,
"is_member": 1
}
Выдает ошибку: Assertion failed: IsObject(), file ...\rapidjson-master\include\rapidjson\document.h, line 1344
Код:
int getIdGroop(std::string GroupName) {
Document documentJson;
std::string formedLink = LINK_TO_THE_API_SERVER + "groups.getById?group_ids=" + GroupName + "&access_token=" + ACCESS_TOKEN + "&v=" + VERSION_API ,readBuffer;
int IdOfGroup = 1;
CURL* curl;
CURLcode response;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, formedLink.c_str());
//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); //Отключение проверки SSL сертификата
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
response = curl_easy_perform(curl);
//curl_easy_cleanup(curl);
if (documentJson.Parse(readBuffer.c_str()).HasParseError())
std::cout << "error " << std::endl;
documentJson.Parse(readBuffer.c_str());
const Value& valueResponse = documentJson["response"];
std::cout << valueResponse["id"].GetInt() << std::endl;
return IdOfGroup;
}
Ответы (1 шт):
Автор решения: Galahad
→ Ссылка
Не заметил что response это массив объектов ( [{}] ), так что нужно указывать [0]
const Value& valueResponse = documentJson["response"];
return documentJson["response"][0]["id"].GetInt();