Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsonCpp - when having a json::Value object, how can i know it's key name?

Tags:

c++

json

jsoncpp

Let suppose I have this Json file:

[
    {
        "id": 0
    }
]

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

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:

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?

like image 754
Rong Avatar asked Dec 10 '13 13:12

Rong


2 Answers

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

Json::Value value;
for (auto const& id : value.getMemberNames()) {
    std::cout << id << std::endl;
}
like image 71
Brandon Avatar answered Sep 28 '22 05:09

Brandon


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.

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

like image 30
cdunn2001 Avatar answered Sep 28 '22 05:09

cdunn2001