1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
void trim_quotes(string& s)
{
if (!s.empty() && s.front() == '"') s.erase(0, 1);
if (!s.empty() && s.back() == '"') s.pop_back();
}
int main()
{
string line {"{\"flight_number\":65,"
"\"mission_name\":\"Telstar 19V\","
"\"mission_id\":[\"F4F83DE\"],"
"\"launch_year\":\"2018\"}"};
istringstream ss(line);
char ch;
ss >> ch; // eat initial '{'
int flight_number;
string mission_name, year, id;
getline(ss, id, ':');
ss >> flight_number;
ss >> ch; // eat ','
getline(ss, id, ':');
getline(ss, mission_name, ',');
trim_quotes(mission_name);
getline(ss, id, ','); // eat entire "mission_id" line
getline(ss, id, ':');
getline(ss, year);
if (!year.empty() && year.back() == '}')
year.pop_back();
trim_quotes(year);
cout << flight_number << '\n' << mission_name << '\n' << year << '\n';
}
|