为了账号安全,请及时绑定邮箱和手机立即绑定

求各位指教一下:rapidjson怎么将数据写入到json文件?

/ 猿问

求各位指教一下:rapidjson怎么将数据写入到json文件?

富国沪深 2019-11-26 10:06:57

rapidjson怎么将数据写入到json文件


查看完整描述

3 回答

?
慕婉清6462132

我来为您解答:
#include <iostream>
#include <errno.h>
#include "fstream"
#include "CocoStudio/Json/rapidjson/stringbuffer.h"
#include "CocoStudio/Json/rapidjson/writer.h"
//---------------------------------------------------------------------------------
void CElemDataMgr::WriteUserElem(int nUserElemId)
{
m_VecUserElem.push_back(nUserElemId);
Document Doc;
Doc.SetObject();
Value vElem;
vElem.SetArray();
int nSize = m_VecUserElem.size();
for (int i = 0; i < m_VecUserElem.size(); ++i)
{
Value vElemItem;
vElemItem.SetInt(m_VecUserElem);
vElem.PushBack(vElemItem, Doc.GetAllocator());
}
Doc.AddMember("Data", vElem, Doc.GetAllocator());
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
Doc.Accept(writer);
std::string strJson(buffer.GetString(), buffer.Size());
CCLOG("BioEvolve::CElemDataMgr:%s", strJson.c_str());
std::string filepath = (CCFileUtils::sharedFileUtils()->getWritablePath() + "UserElem.json");
std::ofstream outfile;
outfile.open(filepath.c_str());
if (outfile.fail())
{
return;
}
outfile << strJson;
outfile.close();
}。

 本回答由电脑网络分类达人 武岩推荐

 1   

 评论 

分享

 举报

一骑当后 
知道合伙人数码行家 2015-01-23

展开全部

您好,我来为您解答:
#include <iostream>
#include <errno.h>
#include "fstream"
#include "CocoStudio/Json/rapidjson/stringbuffer.h"
#include "CocoStudio/Json/rapidjson/writer.h"
//---------------------------------------------------------------------------------
void CElemDataMgr::WriteUserElem(int nUserElemId)
{
m_VecUserElem.push_back(nUserElemId);
Document Doc;
Doc.SetObject();
Value vElem;
vElem.SetArray();
int nSize = m_VecUserElem.size();
for (int i = 0; i < m_VecUserElem.size(); ++i)
{
Value vElemItem;
vElemItem.SetInt(m_VecUserElem);
vElem.PushBack(vElemItem, Doc.GetAllocator());
}
Doc.AddMember("Data", vElem, Doc.GetAllocator());
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
Doc.Accept(writer);
std::string strJson(buffer.GetString(), buffer.Size());
CCLOG("BioEvolve::CElemDataMgr:%s", strJson.c_str());
std::string filepath = (CCFileUtils::sharedFileUtils()->getWritablePath() + "UserElem.json");
std::ofstream outfile;
outfile.open(filepath.c_str());
if (outfile.fail())
{
return;
}
outfile << strJson;
outfile.close();
}


查看完整回答
反对 回复 2019-12-01
?
守着一只汪

用的 C/C++ std::string json_to_string( const rapidjson::Value& object ) { rapidjson::StringBuffer buf; rapidjson::Writer w(buf); object.Accept(w); return std::string(buf.GetString()); }

查看完整回答
反对 回复 2019-12-01
?
繁星coding

#include <iostream>
#include <errno.h>
#include "fstream"
#include "CocoStudio/Json/rapidjson/stringbuffer.h"
#include "CocoStudio/Json/rapidjson/writer.h"
//---------------------------------------------------------------------------------
void CElemDataMgr::WriteUserElem(int nUserElemId)
{
m_VecUserElem.push_back(nUserElemId);
Document Doc;
Doc.SetObject();
Value vElem;
vElem.SetArray();
int nSize = m_VecUserElem.size();
for (int i = 0; i < m_VecUserElem.size(); ++i)
{
Value vElemItem;
vElemItem.SetInt(m_VecUserElem);
vElem.PushBack(vElemItem, Doc.GetAllocator());
}
Doc.AddMember("Data", vElem, Doc.GetAllocator());
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
Doc.Accept(writer);
std::string strJson(buffer.GetString(), buffer.Size());
CCLOG("BioEvolve::CElemDataMgr:%s", strJson.c_str());
std::string filepath = (CCFileUtils::sharedFileUtils()->getWritablePath() + "UserElem.json");
std::ofstream outfile;
outfile.open(filepath.c_str());
if (outfile.fail())
{
return;
}
outfile << strJson;
outfile.close();
}



查看完整回答
反对 回复 2019-12-01

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信