Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json that I am trying to parse, and I am lost Structure Array below top level #1293

Closed
steven5clu884 opened this issue Oct 12, 2018 · 6 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@steven5clu884
Copy link

steven5clu884 commented Oct 12, 2018

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

@theodelrieu
Copy link
Contributor

Hello!

If you want to read the entire array, you can use speedJson.get<std::vector<SpeedEvents>>();.

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Oct 12, 2018
@steven5clu884
Copy link
Author

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 vectEvents = speedJson.get< 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

@steven5clu884
Copy link
Author

steven5clu884 commented Oct 12, 2018

Thank you thank you
Your suggestion triggered something in my brain
This statement Actually Works
std::vector vectEvents = (*iter).get< 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

@theodelrieu
Copy link
Contributor

No problem!

@nlohmann
Copy link
Owner

@steven5clu884 Do you need further assistance or can we close the issue?

@steven5clu884
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants