Skip to content

How can I retrieve uknown strings from json file in my C++ program. #1684

@MerShalik

Description

@MerShalik

I need to parse json file with string names unknown before hand. Here is an example of my input json file:

{"some_data":
{
"some_name_1":
{
"data": { "a":1, "b":2 }
},
"some_name_2":
{
"data": { "a":3, "b":4 }
} } }

Names "some_name_1" and "some_name_2" could be any string. Here is my short program:

int main( void )
{
nlohmann::json j;
std::ifstream infile( "example.json" );
infile >> j;

for ( const auto& block : j["some_data"] )
std::cout << block << std::endl;
}

Here is the output:
{"data":{"a":1,"b":2}}
{"data":{"a":3,"b":4}}

So I can iterate through blocks "some_name_1" and "some_name_2" but I can't get access to the strings themselves.

Note: I do not think this is related to library version but for completeness: am using lib v 3.5.0 on Mac OS 10.13.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions