rapidjson解析与构造实例

 

 


void
rapidjson1(){ rapidjson::StringBuffer s; rapidjson::Writer<rapidjson::StringBuffer> writer(s); writer.StartObject(); // Between StartObject()/EndObject(), writer.Key("hello"); // output a key, writer.String("world"); // follow by a value. writer.Key("t"); writer.Bool(true); writer.Key("f"); writer.Bool(false); writer.Key("n"); writer.Null(); writer.Key("i"); writer.Uint(123); writer.Key("pi"); writer.Double(3.1416); writer.Key("a"); writer.StartArray(); // Between StartArray()/EndArray(), for (unsigned i = 0; i < 4; i++) writer.Uint(i); // all values are elements of the array. writer.EndArray(); writer.EndObject(); // {"hello":"world","t":true,"f":false,"n":null,"i":123,"pi":3.1416,"a":[0,1,2,3]} std::cout << s.GetString() << std::endl; }

 

document.json

{
    "name": "xiaoming",
    "gender": "boy",
    "hobby": [
        "足球",
        "篮球",
        "电影"
    ],
    "score": {
        "数学": 91.5,
        "语文": 95.5,
        "英语": 96
    },
    "lover": {
        "name": "xiaohong",
        "gender": "girl",
        "hobby": [
            "画画",
            "跳舞",
            "唱歌"
        ],
        "score": {
            "数学": 78.5,
            "语文": 89,
            "英语": 90
        }
    }
}

 

void rapidjson2(){

    std::ifstream t("/home/leoxae/document1.json");
//    std::ifstream t("/home/leoxae/CLionProjects/KeekoAIRobot/data/ninstructionRecognition/bcb2_feature.json");
    std::string str((std::istreambuf_iterator<char>(t)),
                    std::istreambuf_iterator<char>());

    rapidjson::Document document;
    document.Parse(str.c_str());

    rapidjson::Value::ConstMemberIterator iter = document.FindMember("name");
    if(iter != document.MemberEnd()){
        cout << "name : " << iter->value.GetString() << endl;
    }

    iter = document.FindMember("gender");
    if(iter != document.MemberEnd()){
        cout << "gender : " << iter->value.GetString() << endl;
    }

    if(document.HasMember("hobby")){
        cout << "hobby : " << endl;
        const rapidjson::Value& childValue = document["hobby"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetString() << endl;
        }
    }

    if(document.HasMember("score")){
        cout << "score : " << endl;
        const rapidjson::Value& childIter = document["score"];
        for(rapidjson::Value::ConstMemberIterator it = childIter.MemberBegin(); it != childIter.MemberEnd(); ++it){
            cout << "    " << it->name.GetString() << " : " << it->value.GetDouble() << endl;
        }
    }

    if(document.HasMember("lover")){
        cout << "lover : " << endl;
        const rapidjson::Value& chileValue = document["lover"];
        rapidjson::Value::ConstMemberIterator chileIter = chileValue.FindMember("name");
        if(chileIter != chileValue.MemberEnd()){
            cout << "    " << "name : " << chileIter->value.GetString() << endl;
        }

        chileIter = chileValue.FindMember("gender");
        if(chileIter != chileValue.MemberEnd()){
            cout << "    " << "gender : " << chileIter->value.GetString() << endl;
        }

        if(chileValue.HasMember("hobby")){
            cout << "    " << "hobby : " << endl;
            const rapidjson::Value& chile2Value = chileValue["hobby"];
            for(rapidjson::SizeType i = 0; i < chile2Value.Size(); ++i){
                cout << "        " << chile2Value[i].GetString() << endl;
            }
        }

        if(chileValue.HasMember("score")){
            cout << "    " << "score : " << endl;
            const rapidjson::Value& child2Iter = chileValue["score"];
            for(rapidjson::Value::ConstMemberIterator it = child2Iter.MemberBegin(); it != child2Iter.MemberEnd(); ++it){
                cout << "        " << it->name.GetString() << " : " << it->value.GetDouble() << endl;
            }
        }
    }

}

 

ninstr.json

{"\u6269\u5c551": [0, 592], 
"\u6269\u5c552": [592, 1232],
 "\u6269\u5c553": [1232, 2073],
"\u6269\u5c554": [2073, 2874], 
"\u6269\u5c555": [2874, 3786], 
"\u6269\u5c556": [3786, 4683],
 "\u6269\u5c557": [4683, 5267], 
"\u6269\u5c558": [5267, 6151], 
"\u6269\u5c559": [6151, 6895], 
"\u6269\u5c5510": [6895, 7694], 
"\u5730\u57ab\u524d\u8fdb": [7694, 8790], 
"\u5730\u57ab\u5de6\u8f6c": [8790, 10237], 
"\u5730\u57ab\u53f3\u8f6c": [10237, 11617], 
"P1": [11617, 12809], "P2": [12809, 13680], 
"\u5f00\u59cb": [13680, 15049], 
"\u7ed3\u675f": [15049, 16417], 
"\u5730\u57ab\u524d\u8fdb\u5c0f": [16417, 17113], 
"\u5730\u57ab\u5de6\u8f6c\u5c0f": [17113, 18433], 
"\u5730\u57ab\u53f3\u8f6c\u5c0f": [18433, 19954], 
"\u5f00\u59cb\u5c0f": [19954, 21242], 
"\u7ed3\u675f\u5c0f": [21242, 22571], 
"\u6682\u505c": [22571, 23785]}

 

void rapidjson3(){

//    std::ifstream t("/home/leoxae/document.json");
//    std::ifstream t("/home/leoxae/CLionProjects/KeekoAIRobot/data/ninstructionRecognition/bcb2_feature.json");
    std::ifstream t("/home/leoxae/ninstr.json");
    std::string str((std::istreambuf_iterator<char>(t)),
                    std::istreambuf_iterator<char>());

    rapidjson::Document document;
    document.Parse(str.c_str());

    //扩展1
    if(document.HasMember("扩展1")){
        cout << "扩展1 : " << endl;
        const rapidjson::Value& childValue = document["扩展1"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }

    //扩展2
    if(document.HasMember("扩展2")){
        cout << "扩展2 : " << endl;
        const rapidjson::Value& childValue = document["扩展2"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //扩展3
    if(document.HasMember("扩展3")){
        cout << "扩展3 : " << endl;
        const rapidjson::Value& childValue = document["扩展3"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //扩展4
    if(document.HasMember("扩展4")){
        cout << "扩展4 : " << endl;
        const rapidjson::Value& childValue = document["扩展4"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //扩展5
    if(document.HasMember("扩展5")){
        cout << "扩展5 : " << endl;
        const rapidjson::Value& childValue = document["扩展5"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //扩展6
    if(document.HasMember("扩展6")){
        cout << "扩展6 : " << endl;
        const rapidjson::Value& childValue = document["扩展6"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //扩展7
    if(document.HasMember("扩展7")){
        cout << "扩展7 : " << endl;
        const rapidjson::Value& childValue = document["扩展7"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //扩展8
    if(document.HasMember("扩展8")){
        cout << "扩展8 : " << endl;
        const rapidjson::Value& childValue = document["扩展8"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //扩展9
    if(document.HasMember("扩展9")){
        cout << "扩展9 : " << endl;
        const rapidjson::Value& childValue = document["扩展9"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //扩展10
    if(document.HasMember("扩展10")){
        cout << "扩展10 : " << endl;
        const rapidjson::Value& childValue = document["扩展10"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //地垫前进
    if(document.HasMember("地垫前进")){
        cout << "地垫前进 : " << endl;
        const rapidjson::Value& childValue = document["地垫前进"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //地垫左转
    if(document.HasMember("地垫左转")){
        cout << "地垫左转 : " << endl;
        const rapidjson::Value& childValue = document["地垫左转"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //地垫右转
    if(document.HasMember("地垫右转")){
        cout << "地垫右转 : " << endl;
        const rapidjson::Value& childValue = document["地垫右转"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //P1
    if(document.HasMember("P1")){
        cout << "P1 : " << endl;
        const rapidjson::Value& childValue = document["P1"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //P2
    if(document.HasMember("P2")){
        cout << "P2 : " << endl;
        const rapidjson::Value& childValue = document["P2"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //开始
    if(document.HasMember("开始")){
        cout << "开始 : " << endl;
        const rapidjson::Value& childValue = document["开始"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //结束
    if(document.HasMember("结束")){
        cout << "结束 : " << endl;
        const rapidjson::Value& childValue = document["结束"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //地垫前进小
    if(document.HasMember("地垫前进小")){
        cout << "地垫前进小 : " << endl;
        const rapidjson::Value& childValue = document["地垫前进小"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //地垫左转小
    if(document.HasMember("地垫左转小")){
        cout << "地垫左转小 : " << endl;
        const rapidjson::Value& childValue = document["地垫左转小"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //地垫右转小
    if(document.HasMember("地垫右转小")){
        cout << "地垫右转小 : " << endl;
        const rapidjson::Value& childValue = document["地垫右转小"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //开始小
    if(document.HasMember("开始小")){
        cout << "开始小 : " << endl;
        const rapidjson::Value& childValue = document["开始小"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //结束小
    if(document.HasMember("结束小")){
        cout << "结束小 : " << endl;
        const rapidjson::Value& childValue = document["结束小"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
    //暂停
    if(document.HasMember("暂停")){
        cout << "暂停 : " << endl;
        const rapidjson::Value& childValue = document["暂停"];
        for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
            cout << "    " << childValue[i].GetInt() << endl;
        }
    }
}

 

void readrapidjson(){
    string strJsonTest = "{\"item_1\":\"value_1\",\"item_2\":\"value_2\",\"item_3\":\"value_3\",\"item_4\":\"value_4\",\"item_arr\":[\"arr_vaule_1\",\"arr_vaule_2\"]}";
    rapidjson::Document docTest;
    docTest.Parse<0>(strJsonTest.c_str());

    if (!docTest.HasParseError())
    {
        for (rapidjson::Value::ConstMemberIterator itr = docTest.MemberBegin(); itr != docTest.MemberEnd(); itr++)
        {
            rapidjson::Value jKey;
            rapidjson::Value jValue;
            rapidjson::Document::AllocatorType allocator;
            jKey.CopyFrom(itr->name, allocator);
            jValue.CopyFrom(itr->value,allocator);
            if (jKey.IsString())
            {
                string name = jKey.GetString();
//                printf("\r\nname: %s\r\n",name.c_str());
                    cout << "name=>>:" << name << endl;
                }
            }
    }

}
View Code

 

posted @ 2019-09-02 17:59  Xu_Lin  阅读(1347)  评论(0编辑  收藏  举报