Skip to content

Best Way to Rescursively Search JSON? #3469

Answered by nlohmann
meowitzher asked this question in Q&A
Discussion options

You must be logged in to vote

You can do something like this:

#include <nlohmann/json.hpp>
#include <iostream>

using json = nlohmann::json;

template<class UnaryFunction>
void recursive_iterate(const json& j, UnaryFunction f)
{
    for(auto it = j.begin(); it != j.end(); ++it)
    {
        if (it->is_structured())
        {
            recursive_iterate(*it, f);
        }
        else
        {
            f(it);
        }
    }
}

int main()
{
    json j = R"({
    "Assets": [
        {
            "Asset": {
                "File": "assets/music/Music.wav",
                "Name": "Music",
                "Type": 3
            }
        },
        {
            "Asset": {
                "File": "assets/images/Ima…

Replies: 2 comments 11 replies

Comment options

You must be logged in to vote
2 replies
@meowitzher
Comment options

@falbrechtskirchinger
Comment options

Comment options

You must be logged in to vote
9 replies
@gregmarr
Comment options

@VolkerBartheld
Comment options

@falbrechtskirchinger
Comment options

@VolkerBartheld
Comment options

@falbrechtskirchinger
Comment options

Answer selected by meowitzher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants