How to work around Poco::JSON::JSONException error on parse api get request input stream

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

我正在编写一段 C++ 代码来检索作为 JSON 对象的 api get 请求,我正在尝试使用 poco 解析器解析该对象。我在 xxx 内存位置遇到 Poco::JSON::JSONException。我不明白,我应该怎么做才能克服这个问题?

这里是代码片段:

 //Run API Get request, this returns an input stream.  This is done 
 with poco code and works well.  I know because I can print out the 
 input stream and it is the JSON object.  I will start from here:

 std::istream& page = session.receiveResponse(response); //page is the 
 input stream response from the api get request

 Poco::JSON::Parser parser;
 Poco::Dynamic::Var result = parser.parse(page);  // This is the line 
 that the debugger stops on and the error is thrown.

Error output

4行错误输出。我在下面的链接中包含了一个屏幕截图。我也在这里输入了它。

Raw Data PROcessing v2.exe 中的 0x00007FFEC15A051C 抛出异常:Microsoft C++ 异常:内存位置 0x0000004E41BBDEE0 处的 Poco::JSON::JSONException

原始数据处理 v2.exe 中的 0x00007FFEC15A051C 抛出异常:Microsoft C++ 异常:[重新抛出] 在内存位置 0x0000000000000000.

原始数据处理 v2.exe 中的 0x00007FFEC15A051C 处抛出异常:Microsoft C++ 异常:Poco::JSON::JSONException 在内存位置 0x0000004E41BBDEE0.

原始数据处理 v2.exe 中 0x00007FFEC15A051C 处的未处理异常:Microsoft C++ 异常:Poco::JSON::JSONException 位于内存位置 0x0000004E41BBDEE0.

我搜索的时间不长,但找不到太多相关信息。有没有办法解决这个问题?来自 api get 请求的输入流中的 JSON 似乎在语法上有错误。有人可以帮我吗?

c++ poco jsonparser jsonexception
2个回答
0
投票

控制台没有显示整个 api get 请求。我可以将其输入浏览器并获得相同的信息,我这样做并将屏幕截图粘贴到下面的链接中。有头信息,格式如下:

这是来自 alphavantage.co 的股票 api

听起来好像我需要编写一些代码来忽略标题?

{ “元数据”:{ “1. 信息”:“盘中(5 分钟)开盘价、最高价、最低价、收盘价和成交量”, "2. 符号": "gib", "3. 最后刷新": "2023-03-15 16:05:00", "4.间隔": "5min", "5. 输出尺寸": "全尺寸", “6.时区”:“美国/东部” }, “时间序列(5 分钟)”:{ “2023-03-15 16:05:00”:{ “1.开盘”:“89.3400”, "2.高": "89.3400", “3.低”:“89.3400”, "4.关闭": "89.3400", “5.音量”:“100” }, “2023-03-15 16:00:00”:{ “1.打开”:“89.0500”, "2.高": "89.3600", “3.低”:“89.0500”, "4.关闭": "89.3100", 《5.卷》:《19864》 },...}

API screenshot


0
投票

从复制流更改为复制到字符串并解析有效的字符串:

  Poco::StreamCopier::copyToString(page, jsonObject);
  Poco::JSON::Parser parser;
  Poco::Dynamic::Var result = parser.parse(jsonObject);
    Poco::JSON::Object::Ptr pObject = 
  result.extract<Poco::JSON::Object::Ptr>();
    std::string price1 = pObject->getValue<std::string>("Time Series 
  (5min)");
    Poco::Dynamic::Var result2 = parser.parse(price1);
    Poco::JSON::Object::Ptr pObject2 = 
  result2.extract<Poco::JSON::Object::Ptr>();
    std::string price2 = pObject2->getValue<std::string>("2023-03-17 
  16:00:00");
    std::cout << price2 << std::endl;

感谢@Tim Roberts

© www.soinside.com 2019 - 2024. All rights reserved.