JsonCpp——当拥有json::Value对象时,我怎么知道它是键名?

时间:2020-11-29 23:47:11

Let suppose I have this Json file:

假设我有这个Json文件:

[
    {
        "id": 0
    }
]

using jsoncpp, i can have a Json::Value object by doing this:

通过使用jsoncpp,我可以通过以下操作获得Json::Value对象:

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

OK, somewhere else in the code, I'm getting that node object, and I want to get some info out of it. I can get its value, like this:

代码中的其他地方,我得到了节点对象,我想从中得到一些信息。我可以得到它的值,像这样:

int node_value = node.asInt();

But how can I get its NAME? (i.e the "id"). It should be something like:

但是我怎么知道它的名字呢?(我。e“id”)。应该是这样的:

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

but I can't find anything similar. Help? How can I get a node's name?

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

2 个解决方案

#1


4  

You can use Json::Value::getMemberNames() to iterate through the names.

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

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

#2


1  

You need an up-pointer? It's not a bad idea, but adding a field for the up-pointer would break binary-compatibility (which is very important). So yes, you need to wrap it.

你需要一个up-pointer吗?这不是一个坏主意,但是为向上指针添加一个字段将会破坏二进制兼容性(这是非常重要的)。是的,你需要把它包起来。

Currently, a sub-value is just a Value, like any other.

目前,子值只是一个值,与其他值一样。

#1


4  

You can use Json::Value::getMemberNames() to iterate through the names.

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

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

#2


1  

You need an up-pointer? It's not a bad idea, but adding a field for the up-pointer would break binary-compatibility (which is very important). So yes, you need to wrap it.

你需要一个up-pointer吗?这不是一个坏主意,但是为向上指针添加一个字段将会破坏二进制兼容性(这是非常重要的)。是的,你需要把它包起来。

Currently, a sub-value is just a Value, like any other.

目前,子值只是一个值,与其他值一样。