#include<iostream>
#include<fstream>
#include <vector>
#include <string>
#define NULL_VAL "null"
using namespace std;
struct Point
{
string x;
string y;
};
struct Tweet
{
Point coordinates;
string created_at;
string name;
string url;
string in_reply_to_user_id;
string text;
};
ofstream& operator << (ofstream& os,const Tweet tweet)
{
os<<"["<<tweet.coordinates.x<<";"<<tweet.coordinates.y<<"]"<<
" , "<<tweet.created_at<<" , "<<tweet.name<<" , "<<tweet.url<<
" , "<<tweet.in_reply_to_user_id<<" , "<<tweet.text<<endl;
return os;
}
Tweet fillTweet(string jsonNode);
string readJsonNode(string fileName);
void main()
{
string jsonNode;
vector<Tweet> tweets;
ofstream ofs("output.tweet.txt",ios::trunc);
ofs<<"[coordinates],created_at,name,url,in_reply_user_id,text\n";
int i=0;
do{
jsonNode = readJsonNode("statuses_002.json");
if(jsonNode == NULL_VAL)
break;
Tweet tweet = fillTweet(jsonNode);
tweets.push_back(tweet);
cout<<"tweet # "<<++i<<endl;
ofs<<tweet;
}while(jsonNode !=& NULL_VAL);
ofs.close();
}
Tweet fillTweet(string jsonNode)
{
Tweet tweet;
string subStr;
size_t curpos = 0;
size_t lastpos =& jsonNode.find(",\"coordinates\":[");
if(jsonNode.find("tus\":{cont") != string::npos)
{
cout<<"bro"<<endl;
}
if(lastpos == string::npos)
{
tweet.coordinates.x = NULL_VAL;
tweet.coordinates.y = NULL_VAL;
}
else
{
lastpos+=strlen(",\"coordinates\":[");
curpos = jsonNode.find(',',lastpos);
subStr = jsonNode.substr(lastpos,curpos-lastpos);
tweet.coordinates.x = subStr;
lastpos = ++curpos;
curpos = jsonNode.find("]",curpos);
tweet.coordinates.y = jsonNode.substr(lastpos,curpos-lastpos);
}
//----------------------------------------------------------
//created_at ()find 2nd entry created_at
lastpos = jsonNode.find("created_at",0);
lastpos+=strlen("created_at");
lastpos = jsonNode.find("created_at",lastpos);
lastpos = jsonNode.find(":",lastpos);
lastpos+=2;
curpos = jsonNode.find("\"",lastpos);
tweet.created_at = jsonNode.substr(lastpos,curpos-lastpos);
//----------------------------------------------------------
//name
lastpos = 0;
curpos = 0;
lastpos = jsonNode.find("\"name\":\"");
lastpos+=strlen("\"name\":\"");
curpos = jsonNode.find("\"",lastpos);
tweet.name = jsonNode.substr(lastpos,curpos-lastpos);
//----------------------------------------------------------
//"url":
lastpos = 0;
curpos = 0;
lastpos = jsonNode.find("\"url\":");
lastpos+=strlen("\"url\":");
subStr = jsonNode.substr(lastpos,4);
if(subStr.find("htt") == string::npos)
{
tweet.url = "null";
}
else
{
lastpos++;//skip "
curpos = jsonNode.find("\"",lastpos);
tweet.url = jsonNode.substr(lastpos,curpos-lastpos);
}
//-----------------------------------------------------------
//in_reply_to_user_id_str
lastpos = 0;
curpos = 0;
lastpos = jsonNode.find("\"in_reply_to_user_id_str\":");
lastpos+=strlen("\"in_reply_to_user_id_str\":");
subStr = jsonNode.substr(lastpos,4);
if(subStr.find("nul") != string::npos)
{
tweet.in_reply_to_user_id = "null";
}
else
{
curpos = jsonNode.find("\"",lastpos);
tweet.in_reply_to_user_id = jsonNode.substr(lastpos,curpos-lastpos);
}
//--------------------------------------------------------------------
//in_reply_to_user_id_str
lastpos = 0;
curpos = 0;
lastpos = jsonNode.find("\"text\":");
lastpos+=strlen("\"text\":");
subStr = jsonNode.substr(lastpos,4);
if(subStr.find("\"") == string::npos)
{
tweet.text= "null";
}
else
{
lastpos++;
curpos = jsonNode.find("\",",lastpos);
tweet.text = jsonNode.substr(lastpos,curpos-lastpos);
}
return tweet;
}
string readJsonNode(string fileName)
{
static size_t gPos = 0;
ifstream ifs;
ifs.open(fileName);
try
{
if(!ifs.is_open())
throw exception("Cannot open file");
ifs.seekg(gPos,ios::beg);
char nextChar;
do
{
nextChar = ifs.get();
if(!ifs.good())
{
throw exception();
}
}while(nextChar != '{');
string jsonNode;
jsonNode.push_back(nextChar);
int depth = 1;
do
{
nextChar = ifs.get();
jsonNode.push_back(nextChar);
if(nextChar == '}')
depth--;
else if (nextChar == '{')
depth++;
if(!ifs.good())
{
cout<<depth;
throw exception();
}
}while(depth > 0);
gPos = ifs.tellg();
ifs.close();
return jsonNode;
}
catch(exception e)
{
ifs.close();
return NULL_VAL;
}
}
Comments
Leave a comment