Skip to content

Instantly share code, notes, and snippets.

@eddie-chinbat
Created January 6, 2016 08:21
Show Gist options
  • Save eddie-chinbat/ba65c05837c16ffaced0 to your computer and use it in GitHub Desktop.
Save eddie-chinbat/ba65c05837c16ffaced0 to your computer and use it in GitHub Desktop.
Twitter json file parser using boost spirit library
#include <string>
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
using namespace std;
using namespace boost::property_tree;
const string jsonfile = "./twitter.json";
/* Below code is only for streamit programming language to get int variables from parsed file. Because Streamit programming language only read int type
struct msg_settings{
string created_at; //Variable is for string test
int64_t id_test; //Variable is for int to int64 convert test
int64_t id_test1; //Variable is for int to int64 convert test
int IDL; //int64 converted to int
int IDU; //int64 converted to int
int in_reply_to_status_id; //int64
int in_reply_to_user_id; //int64
int retweet_count; //int
int user_idl; //int64 converted to int
int user_idu; //int64 converted to int
int user_followers_count; //int
int user_friends_count; //int
int user_favourites_count; //int
int user_statuses_count; //int
void parse(const string &filename);
};
*/
void msg_settings::parse(const string &filename)
{
ptree pt;
read_json( jsonfile, pt );
BOOST_FOREACH( ptree::value_type const& rowPair, pt.get_child( "" ))
{
//Individual messages
cout << rowPair.first << ": " <<pt.get<string>(rowPair.first)<<endl;
BOOST_FOREACH( ptree::value_type const& itemPair, rowPair.second )
{
//nested messages
cout << "\t" << itemPair.first << ": ";
cout << itemPair.second.get_value<string>() <<" ";
BOOST_FOREACH( boost::property_tree::ptree::value_type const& node, itemPair.second )
{
std::cout << node.second.get_value<std::string>() << " ";
}
cout << endl;
}
cout << endl;
}
}
int main()
{
try
{
msg_settings msg;
msg.parse(jsonfile);
cout<<"Success\n";
}
catch(exception &e)
{
cout<<"Error: "<<e.what()<<"\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment