Skip to content

Iterate over string and pair in a JSON file. #2549

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

You must be logged in to vote

The items() function should do the job.

Example:

#include "json.hpp"

using json = nlohmann::json;

int main()
{
    json j = R"({
    "/post_one": {
        "user_one": "sakfjsaljfjoijfoisadjfio@#42asdf",
        "user_two": "dfasfjlsjldkjfl;ksjadflkj@#42asdf"
    },
    "/post_two": {
        "user_foo": "sakfjsaljfjoijfoisadjfio@@$2asdf",
        "user_bar": "sakfjsaljfjoijfoisadjfio@#%#5324"
    }
})"_json;
    
    std::unordered_map<std::string, std::vector<std::pair<std::string, std::string>>> m;
    
    for (const auto & outer : j.items())
    {
        for (const auto & inner : outer.value().items())
        {
            m[outer.key()].emplace_back(inner.key(), inner.value());
…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by nlohmann
Comment options

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

@cxxboy
Comment options

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