gpt4 book ai didi

c++ - JsonCpp - 当有一个 json::Value 对象时,我怎么知道它的键名?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:27:37 25 4
gpt4 key购买 nike

假设我有这个 Json 文件:

[
{
"id": 0
}
]

使用 jsoncpp,我可以通过这样做得到一个 Json::Value 对象:

Json::Value node = root[0u]["id"];

好的,在代码的其他地方,我正在获取那个 node 对象,我想从中获取一些信息。我可以得到它的值(value),像这样:

int node_value = node.asInt();

但是我怎样才能得到它的名字呢? (即“id”)。它应该是这样的:

string node_name  = node.Name(); //or maybe:
string node_name2 = node.Key();

但我找不到类似的东西。帮助?如何获取节点的名称?

最佳答案

您可以使用 Json::Value::getMemberNames() 遍历名称。

Json::Value value;
for (auto const& id : value.getMemberNames()) {
std::cout << id << std::endl;
}

关于c++ - JsonCpp - 当有一个 json::Value 对象时,我怎么知道它的键名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20496231/

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