如何从JSON的特定数据中获取数据

问题描述 投票:0回答:1

我正在尝试获得第一个acceptd_answer_id。如果我打印出已解析的数据,这就是它的样子

{"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}]}

我想要的是第一个acceptd_answer_id的号码。因此,在这种情况下,我想要的只是23249538。我将如何去做?我正在使用堆栈交换API进行此操作。这是给学校的作业,我必须在我们的服务器上设置一个工具,当人们提出问题时,该工具将使他们从栈溢出中获得答案。该号码将使用CURL通过管道传递到另一个URL,然后它将为我抓取答案并将其打印到用户的终端。我还希望,如果没有accepted_answer_id,它将以无法找到用户答案的​​方式终止程序。


                //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;


                }
        }
c++ json
1个回答
0
投票
您可以为CPP使用json库,例如Rapidjson(rapidjson更快)或jsoncpp等。https://github.com/Tencent/rapidjsonhttps://github.com/open-source-parsers/jsoncpp
© www.soinside.com 2019 - 2024. All rights reserved.