I have a text file that's got data written in JSON format. The data looks something like this --
[
...
{
"volume": 93,
"id": "part-30",
"value": 19
},
{
"volume": 83,
"id": "part-31",
"value": 19
}
...
]
After referring to this and this
I've gotten to a point where I can read the "name" field of the following data structure. So all other things aside, my code to read this object looks like this --
// read from parts list file to JSON object.
const char* file_name2( "parts_list.txt" );
ifstream is2( file_name2 );
json_spirit::Value value2;
read( is2, value2 );
// const Object& addr_array = value.get_obj();
vector<Value> jsonObj2 = value2.get_array();
vector<Value>::iterator it;
vector<RobotParts> final;
for(it = jsonObj2.begin(); it!=jsonObj2.end(); it++)
{
auto valObj = it->get_obj();
RobotParts rpObj = RobotParts();
for(auto vo : valObj)
{
if(vo.name_=="volume"){
string s = vo.value_;
}
}
final.push_back(rpObj);
}
cout << final.size() << endl;
return 0;
But this line here --> vo.value_;
seems to be creating a lot of problems.
I'm unable to figure out what's the data type of this object.
So far I've tried :
- Reading into an integer. I thought since the volume has an integer value
int i = vo.get_value< int >();
Should work. But instead, it says
error: no member named 'get_value' in
'json_spirit::Pair_impl<json_spirit::Config_vector<std::__1::basic_string<char> > >'
- Reading into a string so I could print it out.
string s = vo.value_;
This throws the following error:
error: no viable conversion from 'Value_type' (aka
'Value_impl<json_spirit::Config_vector<std::__1::basic_string<char> > >') to 'string' (aka 'basic_string<char, char_traits<char>,
allocator<char> >')
I can, however, print out vo.name_ by using cout << vo.name_ << endl
within the loop.
This outputs :
...
volume
id
value
volume
id
value
...
so on.
I know this is due to data type incompatibility but I've spent two-three hours now unable to figure out how to access this value.
How do I access these values corresponding to volume, id and value?