-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
How to determine the type of elements read from a JSON array? #1564
Comments
You could call |
Thanks! but both examples above are |
You would need to call it for each element. As the array has a begin/end iterator, you can also use |
That makes sense, thanks again. But if I understand it correctly then I would always first need to do a |
No, you can access the JSON vector before this: if (std::all_of(jdesign["vectors"].begin(), jdesign["vectors"].end(), [](const json& el){ return el.is_string(); })
{
// access as string vector, e.g. like
std::vector<std::string> foo = jdesign["vectors"];
} (untested code, but you should get the gist). |
The JSON files I need to read with my program have the form
{ "labels": [ 0, 0, 1, 1 ], "vectors": [ [ 1, 1 ], [ 2, 2 ], [ 2, 4 ], [ 4, 2 ] ] }
or
{ "labels": [ 0, 0, 1, 1 ], "vectors": [ "file1", "file2", "file3", "file4" ] }
to represent specimen that have a numerical label and numerical data, the second of which may be given in the JSONs themselves or read from files.
The labels are read in the line
and for the first form the line
does the trick. But if I want to test for the second form (the values are given as file names) and use
I get:
Is there a way to find out the type of the elements of
vectors
before assigning them to my program variables?I am using Debian Linux (stretch, kernel 4.9.0-8-amd64) and GCC 8.2.0 and the json library as pulled from the GitHub repository.
The text was updated successfully, but these errors were encountered: