使用POCO迭代JSON结构

Iterate on json structure with Poco

本文关键字:结构 JSON 迭代 POCO 使用      更新时间:2023-10-16

我正在使用poco库作为C 代码

这是我必须分析的JSON树的一个示例

{
    "name" : "actionToDo",
    "description" : "",
    "version" : "1",
    "parameters" : {
        "inputDirectory" : "string",
        "workingDir" : "string",
        "tempDir" : "string",
        "size" : "integer"
    }
}

"参数"字段中的数据量可能会更改。我必须将所有项目放入地图

今天我的代码

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 :: [注意]:struct

14:13:00'900 :: [crigine]:异常:异常:无法从文件运行: /opt/.../file.json例外: 无法解析"参数"或其子女

无效访问:不是结构。

"无法解析字段的"参数"或其子女"是由下面的捕获物生成的,但"无效访问:不是结构"。来自poco

用于 collection 变量 dynamicsstruct 而不是 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());
}