iterate and retrieve nested object in JSON using rapidjson

后端 未结 5 1590
情书的邮戳
情书的邮戳 2021-01-12 07:20

I am parsing a JSON structure which is similar as follows

{
    \"item1\" : \"value1\"
    \"item2\" : \"value2\"
    // ...
    \"itemn\" : {
          


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-12 07:50

    a.raya203's post https://stackoverflow.com/a/43120359/6155053 didn't work for me out of the box (doesn't handle all types the current rapidjson version implements, and thus ran into errors when e.g. outputting gltfs containing doubles, etc.) but it brought me on the right track for understanding how rapidjson works for parsing a document, so I thought I'd leave my (updated) code here, maybe it helps out someone else…

    #include 
    #include 
    
    class JsonNodePrinter final
    {
    public: 
        static void PrintNode(const rapidjson::Value &node, size_t indent = 0, unsigned int level = 0, const std::string& nodeName = "")
        {
            std::cout << GetIndentString(indent, level);
    
            if (!nodeName.empty())
                std::cout << nodeName << ": ";
    
            if (node.IsBool())
                std::cout << node.GetBool();
    
            else if (node.IsInt())
                std::cout << node.GetInt();
    
            else if (node.IsUint())
                std::cout << node.GetUint();
    
            else if (node.IsInt64())
                std::cout << node.GetInt64();
    
            else if (node.IsUint64())
                std::cout << node.GetUint64();
    
            else if (node.IsDouble())
                std::cout << node.GetDouble();
    
            else if (node.IsString())
                std::cout << node.GetString();
    
            else if (node.IsArray())
            {
                if (!nodeName.empty()) std::cout << "\n" << GetIndentString(indent, level);
                PrintArray(node, indent, level);
            }
    
            else if (node.IsObject())
            {
                if (!nodeName.empty()) std::cout << "\n" << GetIndentString(indent, level);
                PrintObject(node, indent, level);
            }
    
            std::cout << "\n";
        }
    
        static void PrintObject(const rapidjson::Value &node, size_t indent = 0, unsigned int level = 0)
        {
            std::cout << "{\n";
    
            for (rapidjson::Value::ConstMemberIterator childNode = node.MemberBegin(); childNode != node.MemberEnd(); ++childNode)
            {
                PrintNode(childNode->value, indent, level + 1, childNode->name.GetString());
            }
    
            std::cout << GetIndentString(indent, level) << "}";
        }
    
        static void PrintArray(const rapidjson::Value& node, size_t indent = 0, unsigned int level = 0)
        {
            std::cout << "[\n";
    
            for (rapidjson::SizeType i = 0; i < node.Size(); ++i)
            {
                PrintNode(node[i], indent, level + 1);
            }
    
            std::cout << GetIndentString(indent, level) << "]";
        }
    
        static std::string GetIndentString(size_t indent = 0, unsigned int level = 0)
        {
            return std::move(std::string(level * indent, ' '));
        }
    };
    

    Use it like

    #include "3rdParty/rapidjson/document.h"
    
    rapidjson::Document document;
    {
        document.Parse(FileHelper::ReadString(filePath)->c_str());
    }
    
    if (!document.HasParseError())
    {
        JsonNodePrinter::PrintNode(document, 4);
    }
    

提交回复
热议问题