gpt4 book ai didi

c++ - 使用 boost 读取复杂的 JSON 数组

转载 作者:行者123 更新时间:2023-11-28 04:51:25 28 4
gpt4 key购买 nike

Using boost property tree to read int array 有关

我想用 boost (1.53.0) 读这个:

{
"model_json_version": "333",
"model_actions": [
{
"action_type": "rep_processor",
"rp_type": "basic_cln"
},
{
"action_type": "feat_generator",
"tags": "numeric"
}
]
}

但是 model_actions 数组中对象的属性不会被打印出来!

我的代码:

for (ptree::value_type &p : pt)
MLOG("1. [%s][%s]\n", p.first.c_str(), p.second.data().c_str());
for (ptree::value_type &p : pt.get_child("model_actions")) {
auto& action = p.second;
MLOG("\taction_type [%s]\n", action.get<string>("action_type").c_str());
for (ptree::value_type &attr : action)
MLOG("\t2. [%s][%s]\n", p.first.c_str(), p.second.data().c_str());
}

打印输出:

1. [model_json_version][333]
1. [model_actions][]
action_type [rep_processor]
2. [][]
2. [][]
action_type [feat_generator]
2. [][]
2. [][]

为什么?打印输出 2. 有什么问题?为什么它与 1. 处的打印输出不同?

最佳答案

JSON 数组被解析为“未命名”的子树;因此这个

MLOG("\t2. [%s][%s]\n", p.first.c_str(), p.second.data().c_str());

将空字符串作为键和数据;如果你想要它的 child ,你应该写:

MLOG("\t2. [%s][%s]\n", attr.first.c_str(), attr.second.data().c_str());

关于c++ - 使用 boost 读取复杂的 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48137589/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com