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