Open srikarad07 opened 6 years ago
Since attributes
is an array (you may check it with attributes.IsArray()
, so you should iterates the array first:
assert(attributes.IsArray()); // attributes is an array
for (rapidjson::Value::ConstValueIterator itr = attributes.Begin(); itr != attributes.End(); ++itr) {
const rapidjson::Value& attribute = *itr;
assert(attribute.IsObject()); // each attribute is an object
for (rapidjson::Value::ConstMemberIterator itr2 = attribute.MemberBegin(); itr2 != attribute.MemberEnd(); ++itr2) {
std::cout << itr2->name.GetString() << " : " << itr2->value.GetString() << std::endl;
}
}
Thanks for your answer!
My question was to obtain the value of string such as ('value', 'maximumValue', etc..) for a specific value (Eg: mass) for the string 'name'. However, the above code iterates over all the strings and extract their specific values.
A small edit: In the above code I get an error for using 'rapidjson::Value:: ConstIterator'. I looked around on the rapidjson tutorials and noticed it is 'rapidjson::Value::ConstValueIterator'.
So you need to firstly find which attribute is "mass", and then obtain the properties. I fixed the above example.
Yup, exactly that's what I need to do.
I don't think the above example is fixed as I cannot see where did you obtain the "mass" property. Can you please add it again. Thank you!
assert(attributes.IsArray()); // attributes is an array for (rapidjson::Value::ConstValueIterator itr = attributes.Begin(); itr != attributes.End(); ++itr) { const rapidjson::Value& attribute = *itr; assert(attribute.IsObject()); // each attribute is an object for (rapidjson::Value::ConstMemberIterator itr2 = attribute.MemberBegin(); itr2 != attribute.MemberEnd(); ++itr2) { std::cout << itr2->name.GetString() << " : " << itr2->value.GetString() << std::endl; } }
Since
attributes
is an array (you may check it withattributes.IsArray()
, so you should iterates the array first:assert(attributes.IsArray()); // attributes is an array for (rapidjson::Value::ConstValueIterator itr = attributes.Begin(); itr != attributes.End(); ++itr) { const rapidjson::Value& attribute = *itr; assert(attribute.IsObject()); // each attribute is an object for (rapidjson::Value::ConstMemberIterator itr2 = attribute.MemberBegin(); itr2 != attribute.MemberEnd(); ++itr2) { std::cout << itr2->name.GetString() << " : " << itr2->value.GetString() << std::endl; } }
add judge value is string, avoid crash when value is not string type if (itr2->value.IsString()) std::cout << itr2->name.GetString() << " : " << itr2->value.GetString() << std::endl;
I'm trying to parse the following JSON file in c++. I would like to iterate over the 'attributes' array and obtain the value of the string:'value' for a specific value of a string:'name' of that attribute object. For ex: I would like to parse this JSON file and obtain the 'value' for 'mass'.
I am trying to do this in C++ using rapidjson library to parse the JSON file. Below is my implementation for the parsing of the JSON file. What I would like to do is within the for loop, for a specific 'value' for a string 'name' (for ex: mass) obtain the other 'values' for string such as 'maximumValue', 'minimumValue', 'value' etc.