Replies: 3 comments 2 replies
-
You're on the right track using nlohmann/json. The data you're receiving is not JSON Lines (JSONL) — it's just a normal JSON object. JSON Lines would be multiple JSON objects separated by newlines, like: {"bids": [...], "asks": [...]} #include <nlohmann/json.hpp> using json = nlohmann::json; int main() {
} If you're getting the data from libcurl, make sure you null-terminate the buffer and pass it to json::parse(dataStr). If json::parse() throws, you can catch it with: cpp |
Beta Was this translation helpful? Give feedback.
-
This may be related: https://json.nlohmann.me/features/parsing/json_lines/ |
Beta Was this translation helpful? Give feedback.
-
This is my curl code. I have renamed some of the variables for confidentiality. Somewhere earlier, std::string someHttpData is declared. My callback function is - callbackGetData(char* data, size_t size, size_t nmemb, void* clientp)
} This is my curl code
httpCode gets 200 response. someHttpData gets the JSON data. I need to json parse someHttpData, to get all the data. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
In my C++ project (in Visual Studio 2022), I have used libcurl to get some stock data, with the usual calls to curl_easy_setopt and callback functions etc.
The original json data is of the format
{"bids":
[{"price":"117356.33","amount":"0.00682","timestamp":"1753515568"},
{"price":"117354.34","amount":"0.025566","timestamp":"1753515568"},
{"price":"117354.26","amount":"0.0412153","timestamp":"1753515568"}],
"asks":
[{"price":"117369.23","amount":"0.0000813","timestamp":"1753515568"},
{"price":"117385.67","amount":"0.005","timestamp":"1753515568"}]
}
I get the json lines data in a std::string object.
I am using nlohmann-json package.
I need to parse the data, so that I can get the individual prices, amounts, timestamps etc., and then work on the data, later. The working on the data part is not important.
My question is, how do I parse the data and get the individual values?
The usual json::parse() function does not work on json lines, so far as I know, after reading the other discussions etc.
Thanks a lot in advance for any suggestions.
Beta Was this translation helpful? Give feedback.
All reactions