其他分享
首页 > 其他分享> > jsoncpp中文支持

jsoncpp中文支持

作者:互联网

#include
#include “json/json.h”
#include

using namespace std;
std::string JsonToString(const Json::Value & root)
{
static Json::Value def = {
Json::Value def;
Json::StreamWriterBuilder::setDefaults(&def);
def[“emitUTF8”] = true;
return def;
}();

std::ostringstream stream;
Json::StreamWriterBuilder stream_builder;
stream_builder.settings_ = def;//Config emitUTF8
std::unique_ptr<Json::StreamWriter> writer(stream_builder.newStreamWriter());
writer->write(root, &stream);
return stream.str();

}

int main()
{
cout << “Hello world!” << endl;
Json::StreamWriterBuilder wBuilder;
Json::Value root;
root[“data”]=123;
root[“name”]=“中国”;
root[“emitUTF8”] = true;
wBuilder.setDefaults(&root);

string m_json=root.toStyledString();
string gbjson=JsonToString(root);
cout <<gbjson<<endl;
cout <<m_json;
return 0;

}

输出结果:

Hello world!
{
“commentStyle” : “All”,
“data” : 123,
“dropNullPlaceholders” : false,
“emitUTF8” : false,
“enableYAMLCompatibility” : false,
“indentation” : “\t”,
“name” : “中国”,
“precision” : 17,
“precisionType” : “significant”,
“useSpecialFloats” : false
}
{
“commentStyle” : “All”,
“data” : 123,
“dropNullPlaceholders” : false,
“emitUTF8” : false,
“enableYAMLCompatibility” : false,
“indentation” : “\t”,
“name” : “\u0590\u067a”,
“precision” : 17,
“precisionType” : “significant”,
“useSpecialFloats” : false
}

标签:中文,false,stream,支持,Json,emitUTF8,jsoncpp,root,def
来源: https://blog.csdn.net/zlyadvocate/article/details/122727602