Skip to content

Instantly share code, notes, and snippets.

@mloskot
Created December 22, 2011 11:11
Show Gist options
  • Save mloskot/1509935 to your computer and use it in GitHub Desktop.
Save mloskot/1509935 to your computer and use it in GitHub Desktop.
Simple example of parsing and consuming JSON array with boost::property_tree
#ifdef _MSC_VER
#include <boost/config/compiler/visualc.hpp>
#endif
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <cassert>
#include <exception>
#include <iostream>
#include <sstream>
#include <string>
int main()
{
try
{
std::stringstream ss;
ss << "{ \"root\": { \"values\": [1, 2, 3, 4, 5 ] } }";
boost::property_tree::ptree pt;
boost::property_tree::read_json(ss, pt);
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("root.values"))
{
assert(v.first.empty()); // array elements have no names
std::cout << v.second.data() << std::endl;
}
return EXIT_SUCCESS;
}
catch (std::exception const& e)
{
std::cerr << e.what() << std::endl;
}
return EXIT_FAILURE;
}
@iamfahad43
Copy link

Hi there,
I am searching 1.instrument_name, 2.bids, 3.asks from json below:

{"jsonrpc":"2.0","method":"subscription","params":{"channel":"book.BTC-PERPETUAL.raw","data":{"type":"change","timestamp":1635513739435,"prev_change_id":6807100702,"instrument_name":"BTC-PERPETUAL","change_id":6807100703,"bids":[["new",60772.0,50.0]],"asks":[]}}}

have there any iteration for these three fields?
like I am still confusing because I use your example code.

Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment