Отредактировано, чтобы упростить и показать мой ТОЧНЫЙ код.
У меня есть следующие данные, которые мне нужно сериализовать в JSON, а также проанализировать из JSON.
string name;
std::map<string, string> metaData;
Мне нужно, чтобы JSON был вложенным/иерархическим следующим образом:
{
"name":"john smith"
"metadata":
{
"age":45,
"middle_name":"william",
},
}
Вот мой ТОЧНЫЙ код:
void DeserializeFromJSON(string &jsonString)
{
// Parse the JSON
Poco::JSON::Parser jsonParser;
Poco::Dynamic::Var parsedJSON = jsonParser.parse(jsonString);
Poco::Dynamic::Var parsedResult = jsonParser.result();
// Extract the JSON Object
Poco::DynamicStruct jsonStruct = *parsedResult.extract<Poco::JSON::Object::Ptr>();
// Get the name
name = jsonStruct["name"].toString();
// Get the meta data -- which of these should I use???
Poco::Dynamic::Var metaVar = jsonStruct["metadata"];
Poco::JSON::Object metaObj = jsonStruct["metadata"];
// At this point, exactly what is it I have in 'metaVar' and 'metaObj'?
// If I try to loop like you say, I get compiler error C2664 in "var.h"
for (Poco::JSON::Object::ConstIterator itr = jsonObject.begin(), end = jsonObject.end(); itr != end; ++itr)
{
string metaName = itr->first;
string metaValue = itr->second.toString();
}
}