使用RapidJson在C 中解析一系列对象
Parse an array of objects in C++ using rapidjson
我正在尝试在C 中解析以下JSON文件。我想迭代"属性"数组,并获得字符串的值:"值"的字符串值:该属性对象的"名称"。对于Ex:我想解析此JSON文件并获得"质量"的"值"。
{
"active": true,
"apiTier": 0,
"attributes": [
{
"description": "Given in the datasheet as '0.33 kg (0.73 lbm)'.",
"maximumValue": "",
"measurementUnit": "kg",
"minimumValue": "",
"name": "mass",
"productConfiguration": "base",
"value": "0.33"
},
{
"description": "",
"maximumValue": "",
"measurementUnit": "",
"minimumValue": "",
"name": "propellant-type",
"productConfiguration": "base",
"value": "hydrazine"
},
{
"description": "Given in the datasheet as 'Thrust/Steady State' and the specified value is also reported as '(0.05-0.230) lbf'.",
"maximumValue": "1.02",
"measurementUnit": "N",
"minimumValue": "0.22",
"name": "thrust",
"productConfiguration": "base",
"value": ""
}
]
我正在尝试使用RapidJson库在C 中进行此操作,以解析JSON文件。以下是我对JSON文件解析的实现。我想做的是在for循环中,对于字符串'名称'的特定"值"(for ex:mass(获取字符串的其他"值",例如'maximutvalue','mixumbvalue','minimumvalue','value'等
#include <fstream>
#include <sstream>
#include <string>
#include <rapidjson/document.h>
int main()
{
std::ifstream inputFile( "/path/to/json/file/" );
std::stringstream jsonDocumentBuffer;
std::string inputLine;
while ( std::getline( inputFile, inputLine ) )
{
jsonDocumentBuffer << inputLine << "n";
}
rapidjson::Document config;
config.Parse( jsonDocumentBuffer.str( ).c_str( ) );
assert(config.IsObject());
const rapidjson::Value& attributes = config["attributes"];
assert(attributes.IsArray());
int counter = 0;
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;
}
}
编辑1:我找到了一个关于如何迭代属性数组的解决方案,并访问每个字符串及其值,及其值为属性数组中的每个对象。但是,我想做的是获得任何字符串的值(ex:'maximumValue','miximunvalue','value'(
如果要获得所选属性的指定属性,则可以使用以下函数:
#include <iostream>
#include <vector>
#include <map>
#include "rapidjson/document.h"
std::map<std::string, std::string> mapForAttributeThatMatchesName(const rapidjson::Value& attributes, const std::string& findMemberName, const std::string& findMemberValue, const std::vector<std::string>& keysToRetrieve) {
std::map<std::string, std::string> result;
for (rapidjson::Value::ConstValueIterator itr = attributes.Begin(); itr != attributes.End(); ++itr) {
const rapidjson::Value::ConstMemberIterator currentAttribute = itr->FindMember(findMemberName.c_str());
if (currentAttribute != itr->MemberEnd() && currentAttribute->value.IsString()) {
if (currentAttribute->value == findMemberValue.c_str()) {
for (auto &keyToRetrieve : keysToRetrieve) {
const rapidjson::Value::ConstMemberIterator currentAttributeToReturn = itr->FindMember(keyToRetrieve.c_str());
if (currentAttributeToReturn != itr->MemberEnd() && currentAttributeToReturn->value.IsString()) {
result[keyToRetrieve] = currentAttributeToReturn->value.GetString();
}
}
return result;
}
}
}
return result;
}
您可以这样测试:
const rapidjson::Value& attributes = config["attributes"];
assert(attributes.IsArray());
std::vector<std::string> keysToRetrieve = {"maximumValue", "minimumValue"};
std::map<std::string, std::string> mapForResult = mapForAttributeThatMatchesName(attributes, "name", "mass", keysToRetrieve);
for (auto &mapItem : mapForResult) {
std::cout << mapItem.first << ":" << mapItem.second << "n";
}
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 从主函数中删除在另一个函数中使用 new 创建的一系列对象
- 使用RapidJson在C 中解析一系列对象
- C 在堆栈上对对象(节点)做出一系列指针
- QT解析一系列对象JSON
- 将一系列指针传递到类对象
- 是否有一种有效的方法可以将一系列对象与父/子关系分类
- 对需要变量传递的对象使用一系列指针
- 用于对基于I/O的对象执行一系列操作的设计模式
- c++:如果类禁用赋值操作符,是否有可能创建一系列类对象(使用vector?)