使用POCO迭代JSON结构
Iterate on json structure with Poco
我正在使用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());
}
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 在c++中使用nlohmann从类到json的转换
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- JSON转换为nlohmann JSON-lib中的结构数组
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 如何在C++中表示JSON文档的递归对象结构?
- 如何将 JSON 键值(我使用 cpp boost 解析)存储在相关数据结构中?
- 是否有一个很好的方法可以在C 11中打印出像JSON一样的Trie结构(仅迭代解决方案)的扁平命名空间
- 使用Python将C 结构更改为JSON字符串
- 基于输入变量创建JSON结构
- 使用POCO迭代JSON结构
- C++将不同的JSON解析为一个结构或类