简体   繁体   中英

JsonCpp: how to convert an unordered_map to Json::Value

How to convert an unordered_map in C++ to Json::Value in JsonCpp?

For example I have a

unordered_map<std::string, std::string> map;

and I would like to convert it to Json::Value:

Json::Value myJson = ConvertToJson(map); // not a real code

How to do that?

Beware, Jsoncpp use a map to stroe data. So your unordered map will be ordered inside the Json::Value.

Here it is:

Json::Value ConvertToJson(std::unordered_map<std::string, std::string> map)
{
    Json::Value json;

    for(auto& element : map)
    {
        json[element.first] = element.second;

    }
    return json;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM