I am trying to parse a JSON object with poco parser. I am running into a Poco::JSON::JSONException at xxx memory location. I don't understand, what am I supposed to do to get past this?
Here is the code snippet:
//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.
4 lines of error are outputed. I included a screenshot, which is in the link below. .
Can anyone help me out here?
This is the returned JSON object:
{ "Meta Data": { "1. Information": "Intraday (5min) open, high, low, close prices and volume", "2. Symbol": "gib", "3. Last Refreshed": "2023-03-15 16:05:00", "4. Interval": "5min", "5. Output Size": "Full size", "6. Time Zone": "US/Eastern" }, "Time Series (5min)": { "2023-03-15 16:05:00": { "1. open": "89.3400", "2. high": "89.3400", "3. low": "89.3400", "4. close": "89.3400", "5. volume": "100" }, "2023-03-15 16:00:00": { "1. open": "89.0500", "2. high": "89.3600", "3. low": "89.0500", "4. close": "89.3100", "5. volume": "19864" },...}
Changing from copystream to copy to string and parsing the string worked:
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;
Thanks @Tim Roberts