代码拉取完成,页面将自动刷新
#include <iostream>
#include <string>
#include <sstream>
#include <memory>
#include <jsoncpp/json/json.h>
//数据序列化
bool Serialize(const Json::Value &val, std::string &body)
{
//中间缓冲区,将 Json::Value 格式转换为 std::string 格式
std::stringstream ss;
//定义工厂类对象
Json::StreamWriterBuilder swb;
// 设置 emitUTF8 使其输出原始的 UTF-8 编码而不是转义序列
swb["emitUTF8"] = true;
std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
//如果 write 返回值不为零,则错误
int ret = sw->write(val, &ss);
if (ret != 0)
{
std::cout << "Serialize Error!";
return false;
}
body = ss.str();
return true;
}
int main()
{
const char* name = "小明";
const char*sex = "男";
int age = 18;
int score[3] = { 100, 110, 96 };
Json::Value student;
std::string body;
student["姓名"] = name;
student["性别"] = sex;
student["年龄"] = age;
student["成绩"].append(score[0]);
student["成绩"].append(score[1]);
student["成绩"].append(score[2]);
Serialize(student, body);
//打印字符串信息
std::cout << body << std::endl;
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。