How can I get property name and property value from json-cpp parser?

I am using jsoncpp parser (http://jsoncpp.sourceforge.net) to parse JSON data. So, if we have the following JSON:

{ "name": "Joseph", "age": 20 } 

How can I get the property name and value of Joseph, ... after age and 20? Well, we can do universally :

 string e = root.get(propertyName, defaultValue).asString(); 

Put the real one that we want:

 string e = root.get(name, "Mark").asString(); 

Now the variable e is Joseph, it works. But I have to take / write " name ". I don't want QUERY (without asking for the function that I want to get "name" (property name) and "Joseph" (property value)).

After it would be better to keep in the field (e.g. C / C ++):

 property[name][0] = "Joseph" property[age][0] = 20 

How can i do this? Or any other ideas?

+4
source share
1 answer

You can get all the member names of the Json :: Value object using the getMemberNames() function. This returns an object that you can .begin() over with .begin() and .end() , like any other standard library container. (In fact, the return type is an alias for std::vector<std::string> .)

Once you have the names of the participants, you can go through them and use .get(std::string &, const ValueType &) , as you already did, to get the values ​​for each of the keys of the objects.


Note that JSON objects are inherently unordered, so you cannot rely on this list of names that has any order. If you want an ordered object, you should use JSON arrays, not JSON objects.

+5
source

Source: https://habr.com/ru/post/1442216/


All Articles