使用C++将三个string转换为 json string
您可以使用第三方库jsoncpp来将三个string转换为json string,示例如下:
#include <iostream>
#include <json/json.h>
int main() {
std::string str1 = "Hello";
std::string str2 = "world";
std::string str3 = "!";
// 创建Json对象
Json::Value root;
// 将三个字符串添加到Json对象中
root["str1"] = str1;
root["str2"] = str2;
root["str3"] = str3;
// 将Json对象转换为Json字符串
std::string jsonStr = root.toStyledString();
// 输出Json字符串
std::cout << jsonStr << std::endl;
return 0;
}
输出结果为:
{
"str1" : "Hello",
"str2" : "world",
"str3" : "!"
}
注意:在使用该方法前,需要先安装jsoncpp库。
原文地址: https://www.cveoy.top/t/topic/bjgb 著作权归作者所有。请勿转载和采集!