您可以使用第三方库 jsoncpp 将三个字符串转换为 JSON 字符串,示例如下:

#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 库。

C++ 使用 jsoncpp 库将三个字符串转换为 JSON 字符串

原文地址: https://www.cveoy.top/t/topic/mLk1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录