Skip to content

How to determine the type of elements read from a JSON array? #1564

@amwink

Description

@amwink

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

std::vector <float> *y = 
     new std::vector <float> ( jdesign["labels"].get<std::vector<float>>() );

and for the first form the line

std::vector<std::vector<float>> 
     xtest1 ( jdesign["vectors"].get<std::vector<std::vector<intensity>>>() );

does the trick. But if I want to test for the second form (the values are given as file names) and use

std::vector<std::string> 
     xtest2 ( jdesign["vectors"].get<std::vector<std::string>>() );

I get:

terminate called after throwing an instance of 'nlohmann::detail::type_error'
what():  [json.exception.type_error.302] type must be string, but is array`

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions