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; } } } }