【发布时间】:2015-05-14 22:49:57
【问题描述】:
经过编辑以简化并显示我的 EXACT 代码。
我有以下数据需要序列化为 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();
}
}
【问题讨论】:
标签: json c++11 poco-libraries