First thank you for contributing this jason.hpp it is my hope and desire to become very familiar with the utilization of this code. I have spent nearly 6 hourse trying to figure out how to get the second level parsing to work, and have had no luck.
So I am asking for help.
First the json
{
"vel_offset": 3,
"SU_events": [
{
"framecount": 0,
"timestamp": 1.5330738200799999e+09,
"speed": 0
},
{
"framecount": 0,
"timestamp": 1.5330738211800001e+09,
"speed": 0
},
{
"framecount": 0,
"timestamp": 1.5330738221800001e+09,
"speed": 0
},
{
"framecount": 0,
"timestamp": 1.5330738232790000e+09,
"speed": 0
},
{
"framecount": 0,
"timestamp": 1.5330738242790000e+09,
"speed": 0
},
{
"framecount": 0,
"timestamp": 1.5330738252790000e+09,
"speed": 0
},
{
"framecount": 0,
"timestamp": 1.5330738263780000e+09,
"speed": 0
}
]
}
I have from your example been able to read the SU_events using the following function
bool vectorizeSpeedJson(std::string pathToSpeedJson)
{
bool ret = false;
std::ifstream jsonStream(pathToSpeedJson);
nlohmann::json speedJson;
nlohmann::json speedJson2;
jsonStream >> speedJson;
std::cerr << "Size : " << speedJson.size() << std::endl;
for (nlohmann::json::iterator it = speedJson.begin(); it != speedJson.end(); ++it)
{
std::cerr << "iterator key : " << it.key() << std::endl;
// the iterator is the entire SU_events array
if (SuEvents.compare(it.key()) == 0)
{
//std::cerr << "iterator valkue : " << it.value() << std::endl;
if (it.value().is_array())
{
int64_t i64 = it.value().get["timestamp"];
}
}
}
return ret;
}
In my case it.key() is SU_events, and it.Value contains the entire array of values that should belong in the array
I have also defined the following in my cpp file
const std::string SuEvents = "SU_events";
//The struct that defines the SU_events array
typedef struct
{
int framecount;
boost::int64_t timestamp;
int kph;
}SpeedEvent;
void to_json(nlohmann::json& j, const SpeedEvent& speedevent) {
j = nlohmann::json{ {"framecount",speedevent.framecount}, {"timestamp", speedevent.timestamp}, {"speed",speedevent.kph} };
}
void from_json(const nlohmann::json& j, SpeedEvent& speedevent) {
j.at("framecount").get_to(speedevent.framecount);
j.at("timestamp").get_to(speedevent.timestamp);
j.at("speed").get_to(speedevent.kph);
}
std::vector<SpeedEvent> vectSpeedEvents;
So my question is how I can parse, or read the array contents of the it.value ??
My deepest respect, and my best wishes to you and your loved ones
Hello!
If you want to read the entire array, you can use speedJson.get<std::vector<SpeedEvents>>();.
I thank you for your response.
However because of the multi-leveled nature of this JSON, doing as you propose with this statement
std::vector
Throws an exception with the following statement
Exception in JSON WORK:[json.exception.type_error.302] type must be array, but is object
If you look at the JSON I provided above the array occurs under the SU_Events entry.
nlohmann::json::iterator iter = speedJson.find(SuEvents);
So I can not use the iter.value.get, because that is not available at that level
I have tried using iter.value in string streams to get to the next level Array, but .dump, and other string functions result in the next level speedJson2 being garbage.
So you see I am still not able to get at the lower level array, so this problem is not fixed for me.
But i will keep trying
Thank you thank you
Your suggestion triggered something in my brain
This statement Actually Works
std::vector
Praise GOD Halleluia I can now continue on with my work, since I now have a properly formatted
std::vector of SpeedEvents
Could someone, who is more knowledgeable than put together an example, so other's will be able to see that example ??
I now consider this question closed
Thank you again
No problem!
@steven5clu884 Do you need further assistance or can we close the issue?
Yes I am fully pleased with what I have written. As i said in my comments an example of this type would be nice for others to use, and look at.
Again thank you for this fine piece of software
Most helpful comment
Thank you thank you vectEvents = (*iter).get< std::vector>();
Your suggestion triggered something in my brain
This statement Actually Works
std::vector
Praise GOD Halleluia I can now continue on with my work, since I now have a properly formatted
std::vector of SpeedEvents
Could someone, who is more knowledgeable than put together an example, so other's will be able to see that example ??
I now consider this question closed
Thank you again