nlohmann json 内存位置解析错误

nlohmann json parse error at memory location

我正在制作一个 C++ 程序,它将(最终)从 api 端点上的每个页面获取一些信息,然后将每个页面添加到一个数组中。我使用 cpr 作为我的请求库,它在我需要时正确获取页面,然后我使用 nlohmann 的 json 库来解析 json 页面结果,然后在以后使用它。

我的代码:

#include <iostream>
#include <cpr/cpr.h>
#include <nlohmann/json.hpp>
#include <Windows.h>
int main()
{
    using namespace nlohmann;
    auto response = cpr::Get(cpr::Url{ "https://api.hypixel.net/skyblock/auctions" });  // |
    json res = json::parse(response.text);                                              // | These 3 lines will get the number of pages 
    int iNumPages = res["totalPages"];                                                  // |
    json* arrPages = new json[iNumPages];            //Define the array with the number of pages
    for (int x = 0; x < iNumPages; x++) {
        std::cout << x; //Just to see which page the program gets to
        auto pageRes = cpr::Get(cpr::Url{ "https://api.hypixel.net/skyblock/auctions?page="+x });  // | These two lines should take the json response from the 
        arrPages[x] = json::parse(pageRes.text);                                                   // | api and put it into the array in the place of the page no
    }
}

我的问题是,在 for 循环中,程序能够在崩溃并抛出以下错误之前到达第 1 页或第 2 页(页面从 0 开始,因此它可以执行大约 2-3 页):

Unhandled exception at 0x7627E7B2 in xxxxxxxxxx.exe: Microsoft C++ exception: nlohmann::detail::parse_error at memory location 0x0021F450.

不可否认,我不擅长调试,并且有点自学 C++,所以我对它的工作原理的了解肯定有很多差距,但是我尝试了不同的方式将数据放入数组,例如将数组定义为 json arrPages[99],以防数组出现问题,但此问题仍然存在。

我真的希望有人可以分享有关如何解决此问题的任何知识,在此先感谢您。祝你有美好的一天!

您的代码:

"https://api.hypixel.net/skyblock/auctions?page=" + x;

将产生一个字符串序列:

"https://api.hypixel.net/skyblock/auctions?page="
"ttps://api.hypixel.net/skyblock/auctions?page="
"tps://api.hypixel.net/skyblock/auctions?page="
"ps://api.hypixel.net/skyblock/auctions?page="
"s://api.hypixel.net/skyblock/auctions?page="

这不是串联!尝试使用 std::string

std::string("https://api.hypixel.net/skyblock/auctions?page=") + std::to_string(x)