使用RapidJSON解析C ++中的对象数组

斯里卡拉德

我正在尝试在c ++中解析以下JSON文件。我想遍历'attributes'数组并获取该属性对象的string:'name'的特定值的string:'value'的值。例如:我想解析此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循环中进行操作,对于字符串“名称”(例如:mass)的特定“值”,获取字符串的其他“值”,例如“ maximumValue”,“ 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:我找到了一种关于如何遍历属性数组并访问属性数组中每个对象的每个字符串及其值的解决方案。但是,我想做的是获取字符串“名称”的特定值(例如质量)的任何字符串(例如:“ maximumValue”,“ minimumValue”,“ 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";
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用RapidJson访问数组(C ++)

来自分类Dev

解析对象中的数组

来自分类Dev

如果键有效,如何检查 Json 文件并使用 RapidJson 在 C++ 中创建对象数组

来自分类Dev

在Objective C中解析对象的JSON数组

来自分类Dev

解析字典中的对象数组

来自分类Dev

从可观察对象解析数组中的对象

来自分类Dev

JSON - 对象数组中的对象 - 如何使用 JS 解析循环?

来自分类Dev

使用数组根对象在Swift中解析JSON数组

来自分类Dev

在C ++中使用RapidJSON访问JSON数组的字段

来自分类Dev

从C ++中的对象获取数组

来自分类Java

Java与C ++中的对象数组

来自分类Dev

遍历C ++中的对象数组

来自分类Dev

C ++中的对象的多维数组

来自分类Dev

C#中的对象数组

来自分类Dev

使用reactjs数组中的对象

来自分类Dev

使用NSDictionary对象从数组中获取对象

来自分类Dev

如何使用javascript过滤对象数组中的对象数组?

来自分类Dev

使用Jquery或JavaScript从对象数组中删除对象数组

来自分类Dev

C++ RapidJson 帮助反序列化数组对象

来自分类Dev

在RapidJson中循环遍历数组并获取对象元素

来自分类Javascript

解析JavaScript中的JSON对象数组

来自分类Dev

从python中的对象解析Json数组

来自分类Dev

JSON.parse:解析对象中的数组

来自分类Dev

php 解析对象细节中的数组

来自分类Dev

解析Crystal lang中的JSON对象数组

来自分类Dev

从组件中的服务解析JSON数组对象

来自分类Dev

在GO中解析json对象数组

来自分类Dev

解析NodeJS中的JSON对象数组

来自分类Dev

解析Android:从数组中删除对象