vendredi 15 mai 2015

How to deserialize a std::map from JSON using the Poco C++ libraries

This is related to another question I asked, but much more specific. I need to deserialize a std::map from my JSON data.

{
    "stdmap_data":
    {
        "age":45,
        "middle_name":"william"
    }
}

I've tried many, many ways to do this and nothing seems to work. Based on answers to a prior thread, I thought this code should work.

std::map myMap;
std::string jsonString; // string containing the above JSON
Poco::JSON::Parser jsonParser;
Poco::Dynamic::Var parsedJSON = jsonParser.parse(jsonString);
Poco::Dynamic::Var parsedResult = jsonParser.result();
Poco::DynamicStruct jsonStruct = *parsedResult.extract<Poco::JSON::Object::Ptr>();
Poco::Dynamic::Var mapVar = jsonStruct["stdmap_data"];
for (Poco::Dynamic::Var::ConstIterator itr = mapVar.begin(), end = mapVar.end(); itr != end; ++itr)
    myMap[itr->first().toString()] = itr->second().toString();

Unfortunately, this iterator does not have "first" and "second" members. I've tried DynamicStruct (same problem) and JSON::Object (can't/won't convert from jsonStruct["stdmap_data"]).

This seems like something VERY common in JSON data, so I'd be shocked if Poco C++ can't do it. Anyone know how it should be done?

Aucun commentaire:

Enregistrer un commentaire