10

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?

Rong
  • 103
  • 1
  • 1
  • 6

2 Answers2

14

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;
}
Brandon
  • 724
  • 5
  • 12
  • But this means I have to get to the parent of the Value object I have (Which is another unanswered question of mine). IS there a way to get to a Value object's parent? – Rong Dec 10 '13 at 15:10
  • There is no way to get the parent of a Value as far as I know; why do you need it? What is your use case? – Brandon Dec 10 '13 at 23:03
  • 1
    I've been asked to write a wrapper to the jsoncpp library, with a way to traverse the tree of the json. I assumed jsoncpp already took care of that and I just have to find out how. – Rong Dec 11 '13 at 08:22
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.

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

cdunn2001
  • 17,657
  • 8
  • 55
  • 45