使用 Poco 迭代 json 结构
Iterate on json structure with Poco
我正在为 C++ 代码使用 Poco 库
这是我必须解析的 json 树的示例
{
"name" : "actionToDo",
"description" : "",
"version" : "1",
"parameters" : {
"inputDirectory" : "string",
"workingDir" : "string",
"tempDir" : "string",
"size" : "integer"
}
}
"parameters" 字段中的数据量可以更改。
我必须将所有项目放入地图
这是我今天的代码
std:: map<std::string, std::string> output;
Poco::JSON::Parser sparser;
Poco::Dynamic::Var result = sparser.parse(jsonStr);
Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>();
Poco::DynamicStruct ds = *object;
Poco::Dynamic::Var collection(ds["parameters"]);
if (collection.isStruct())
{
LOG("STRUCT"); //it's logged !!
}
for (Poco::Dynamic::VarIterator it = collection.begin(); it != collection.end(); ++it)
{
LOG_F("item : %s", it->toString()); //never logged
//here I would like to have something like
//output[it->first()] = it->second();
}
我得到的输出
14:13:00'900 : : [Notice] : STRUCT
14:13:00'900 : : [Critical] : Exception : Exception: Unable to load Run from file :
/opt/.../file.json Exception:
Unable to parse field 'parameters' or its children
Invalid access: Not a struct.
"Unable to parse field 'parameters' or its children" 由下面的捕获生成,但 "Invalid access: Not a struct." 来自 Poco
用于集合变量DynamicStruct而不是Var
Poco::DynamicStruct collection = ds["parameters"].extract<Poco::DynamicStruct>();
for (auto it = collection.begin(); it != collection.end(); ++it)
{
LOG_F("item : %s", it->second.toString().c_str());
}
我正在为 C++ 代码使用 Poco 库
这是我必须解析的 json 树的示例
{
"name" : "actionToDo",
"description" : "",
"version" : "1",
"parameters" : {
"inputDirectory" : "string",
"workingDir" : "string",
"tempDir" : "string",
"size" : "integer"
}
}
"parameters" 字段中的数据量可以更改。 我必须将所有项目放入地图
这是我今天的代码
std:: map<std::string, std::string> output;
Poco::JSON::Parser sparser;
Poco::Dynamic::Var result = sparser.parse(jsonStr);
Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>();
Poco::DynamicStruct ds = *object;
Poco::Dynamic::Var collection(ds["parameters"]);
if (collection.isStruct())
{
LOG("STRUCT"); //it's logged !!
}
for (Poco::Dynamic::VarIterator it = collection.begin(); it != collection.end(); ++it)
{
LOG_F("item : %s", it->toString()); //never logged
//here I would like to have something like
//output[it->first()] = it->second();
}
我得到的输出
14:13:00'900 : : [Notice] : STRUCT
14:13:00'900 : : [Critical] : Exception : Exception: Unable to load Run from file : /opt/.../file.json Exception: Unable to parse field 'parameters' or its children
Invalid access: Not a struct.
"Unable to parse field 'parameters' or its children" 由下面的捕获生成,但 "Invalid access: Not a struct." 来自 Poco
用于集合变量DynamicStruct而不是Var
Poco::DynamicStruct collection = ds["parameters"].extract<Poco::DynamicStruct>();
for (auto it = collection.begin(); it != collection.end(); ++it)
{
LOG_F("item : %s", it->second.toString().c_str());
}