Reputation: 63
I'm trying to get the first accepted_answer_id. If i print out the parsed data this is what it looks like
{"items":[{"accepted_answer_id":23249538,"question_id":23248046},{"accepted_answer_id":5582009,"question_id":5581697},{"accepted_answer_id":43114369,"question_id":43113569},{"accepted_answer_id":12120575,"question_id":12120425},{"question_id":22162858},{"accepted_answer_id":10101621,"question_id":10101556}]}
what I want is that first accepted_answer_id's numbers. So in this case all I want is 23249538. How would I go about doing this? I'm doing this using stack exchange API. This is for an assignment for school where I have to set up on our server a tool that when people ask a question gets them if available someones answer from stack overflow. This number will piped into another URL using CURL which will then grab the answer for me and print it to terminal for the user. I also want it that if there is no accepted_answer_id it just terminates the program with a failure to find answer to user.
//actually getting the data
result = curl_easy_perform(curl);
//making sure that the curl is ok and worked right
if(result != CURLE_OK)
{
cout << curl_easy_strerror(result) << endl;
return 1;
}
//making sure that the website gave info
int httpCode(0);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
curl_easy_cleanup(curl);
//if website worked it will be equal to 200
if(httpCode == 200)
{
cout << "Sucessful Response From URL" << endl;
json data = json::parse(readBuffer);
cout << data << endl;
}
}
Upvotes: 0
Views: 54
Reputation: 26
You can use a json lib for CPP, such as rapidjson (rapidjson is faster)or jsoncpp, etc. https://github.com/Tencent/rapidjson https://github.com/open-source-parsers/jsoncpp
Upvotes: 1