简体   繁体   English

如何在相关数据结构中存储JSON键值(已使用cpp boost解析)?

[英]How do I store a JSON key values (which I have parsed with cpp boost) in relevant data structure?

My Json has some this sort of format for now (which can of course change later) 我的Json目前有这种格式(当然以后可以更改)

{key1 : value1,
 key2 : value2,
 key3 :{key31 : value31,
        key32 : value32,
        key33 : value33
        },

 key4 : {key41 : value41;
         key42:[ {key4a: value4a,
                  key4b: value4b,
                  key4c: {key4d: value 4d},
                          key4e: [v1 ,v2 ,v3]
                          } ,...can be more values here ]
        }
}

To traverse it I am using: 要遍历它,我正在使用:

#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
#include "boost/foreach.hpp"


void traverse(boost::property_tree::ptree pt){
    using boost::property_tree::ptree;

    for (ptree::value_type &v : pt)
    {
        std::cout<<v.first<<" - "<<v.second.data()<<std::endl;


        if (v.second.size() >= 1){
            traverse(v.second);
        }
    }
}

With this I am able to touch each and every node of my Json. 有了这个,我就能触及Json的每个节点。 I am looking for a better approach to parse and store the Json Key:values now. 我正在寻找一种更好的方法来解析和存储Json Key:values。

I think you underestimate the ability of property tree. 我认为您低估了属性树的能力。 It's a great tool to store and access json and xml like infos and that's why it's introduced with json_parser and xml_parser in boost. 这是一个像信息一样存储和访问json和xml的好工具,这就是为什么在boost中将json_parser和xml_parser引入的原因。

Here Accessing values using a boost::property_tree::string_path in the question you can see a working example of how to access values in property tree with path-like strings. 在此问题中, 使用boost :: property_tree :: string_path访问值时,您可以看到一个有效的示例,该示例说明如何使用类似路径的字符串访问属性树中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将派生类的对象存储在可以访问基本 class 没有的属性的数据结构中? - How can I store objects of derived classes within a data structure from which can be accessed attributes which the base class does not have? 哪个是在 c++ 中存储 1 个键和 2 个值的最佳数据结构 - Which is the best data structure to store 1 key and 2 values in c++ 如何将 const byte * 反序列化为 cpp 中的结构? - How do I deserialise a const byte * to a structure in cpp? 如何存储和获取结构队列? - how do i store and get a queue of structure? 如何优雅地更新通过 Visual Studio 中的 NuGet 获得的提升? - How do I gracefully update boost which have been got via NuGet in Visual Studio? 我如何反转 cpp 中以零开头的数字? - how do i reverse a number which starts with zero in cpp? 如何选择要在XCode中运行的.cpp文件? - How do I choose which .cpp file to run in XCode? C++ 如何在包含两个变量的结构中为我的变量存储不同数量的值? - C++ How do I store a different number of values to my variables in a structure that contains two variables? 如何存储按键? - How do I store key-strokes? 如何在 cpp 中检查给定输入的数据类型? - how do I check the data type of the given input in cpp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM