prettyJson V7.1 使用
?
頭文件
#include "document.h" #include "prettywriter.h" #include "filereadstream.h" #include "filewritestream.h" #include "stringbuffer.h"using namespace rapidjson;
?
輸出json
std::map<int, POINT> shootMap ; string strJson; std::vector<int> everyLoopCount;string str="abc"; int i=0;Document document;Document::AllocatorType& allocator = document.GetAllocator();Value root(kObjectType);Value fixedPoint(kArrayType);//map讀取Value everyLoop(kArrayType);//vector讀取string strX;string strY;string strloop; //map讀取,里面存著POINT類(lèi)型 for (auto iter = shootMap.begin(); iter != shootMap.end(); ++iter){Value arrayBody(kArrayType);strX = to_string(iter->second.x);item.SetString(strX.c_str(), strX.size(), allocator);arrayBody.PushBack(item, allocator);strY = to_string(iter->second.y); item.SetString(strY.c_str(), strY.size(), allocator);arrayBody.PushBack(item, allocator);fixedPoint.PushBack(arrayBody, allocator);}//vector 讀取,里面存著int類(lèi)型for (auto it = everyLoopCount.begin(); it != everyLoopCount.end(); ++it){Value arrayBody(kArrayType);strloop = to_string(*it);item.SetString(strloop.c_str(), strloop.size(), allocator);arrayBody.PushBack(item, allocator);everyLoop.PushBack(arrayBody, allocator);}//map輸出root.AddMember("map", fixedPoint, allocator);//字符串輸出root.AddMember("字符串", StringRef(str.c_str()), allocator);//vectorroot.AddMember("vector", everyLoop, allocator);//introot.AddMember("int", i, allocator);StringBuffer buffer;Writer<StringBuffer> writer(buffer);root.Accept(writer);strJson = buffer.GetString();?
讀取json
Document doc;doc.Parse<0>(strJson.c_str());Value & map= doc["map"];
Value & str= doc["字符串"];
Value & vector= doc["vector"];
Value & int= doc["int"];
?
//輸出到map里
POINT targetPoint;
string temp;
int nKey = 0;
if (map.IsArray())
{
for (size_t i = 0; i < map.Size(); ++i)
{
Value & v = map[i];
if (v.IsArray())
{
Value& col = v[0];
temp = col.GetString();
targetPoint.x = atoi(temp.c_str());
col = v[1];
temp = col.GetString();
targetPoint.y = atoi(temp.c_str());
}
rMap.insert(map<int, POINT>::value_type(nKey, targetPoint));
++nKey;
}
}
temp.empty();
?
//vector
if (scoreValue.IsArray())
{
for (size_t i = 0; i < scoreValue.Size(); ++i)
{
Value & v = scoreValue[i];
if (v.IsArray())
{
Value& col = v[0];
temp = col.GetString();
scores = atof(temp.c_str());
}
score->push_back(scores);
}
}
string str=字符串.GetString();
int i=i.Getint();
?
轉(zhuǎn)載于:https://www.cnblogs.com/ye-ming/p/8797833.html
總結(jié)
以上是生活随笔為你收集整理的prettyJson V7.1 使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: css中实现ul两端的li对齐外面边缘
- 下一篇: mysql 基础配置经验