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 to get the object name? #1936

Closed
3038922 opened this issue Feb 11, 2020 · 4 comments
Closed

How to get the object name? #1936

3038922 opened this issue Feb 11, 2020 · 4 comments

Comments

@3038922
Copy link

3038922 commented Feb 11, 2020

const json userData = {
    {
        "intake",
        {
            {"motor", {
                         {"L", {{"port", 9}, {"test", false}, {"gear", 0}}},
                         {"R", {{"port", 10}, {"test", true}, {"gear", 0}}},
                     }},
            {"ADI", {
                        {"port", 4},
                    }},
            {"pragma", {
                         {"val1", 2600},
                         {"val2", 40},
                         {"val3", 5},
                     }},
        },
    },
};
auto temp=userData ["intake"];
std::cout<<temp.????<<std::endl;

I hope he can output 'intake'.
Which function should I use to get the name of the object name?

@nlohmann
Copy link
Owner

That’s unfortunately not possible. There was an issue in the past, but the solution would have taken a lot of memory overhead.

@3038922 3038922 closed this as completed Feb 11, 2020
@Krzmbrzl
Copy link

Can't the object's name be printed by means of iterators (doc)?

From the docs:

for (auto it = j.begin(); it != j.end(); ++it)
{
    std::cout << it.key() << " : " << it.value() << std::endl;
}

That appears to me as if by iterating over the top-level json object and then using the key() function on the iterator, you should be able to fetch the desired name 🤔

@nlohmann
Copy link
Owner

No, the name belongs to the enclosing object rather to the value itself.

@Krzmbrzl
Copy link

Ah yes, I think I misread the question more towards the direction in which I was searching for something. My bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants