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

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

Closed
MerShalik opened this issue Jul 23, 2019 · 2 comments
Closed

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

MerShalik opened this issue Jul 23, 2019 · 2 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@MerShalik
Copy link

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

@nlohmann
Copy link
Owner

This can be achieved with the items function.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jul 24, 2019
@MerShalik
Copy link
Author

Thank you! It works great.

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

2 participants