将普通 C++ 字符串写入 Rapid JSON 会导致字符串带有反斜杠

Writing normal C++ String to Rapid JSON is resulting in string with backslash

string str = {"appId":"com.mymusic.app","Connectivity":True,"DistractionLevel":2,"display":True};

if(!str.empty())
{
StringBuffer bf;
PrettyWriter<StringBuffer> writer (bf);
writer.StartObject();
writer.Key("info"));
writer.String(str.c_str());
writer.EndObject();    
cout<<"request string is:" , (char *)bf.GetString());

}

cout 正在打印以下带反斜杠的行

{"info":"\"appId\":\"com.mymusic.app\",\"checkConnectivity\":True,\"driverDistractionLevel\":2,\"display\":True}"}

我期待的是

{"info": {"appId":"com.mymusic.app","Connectivity":True,"DistractionLevel":2,"display":True} }

您使用了错误的功能。 String 函数将向 json 对象添加一个字符串值,在这种情况下," 转义为 \" 是预期的。

我认为您真正想要做的是将字符串添加为 json-子对象。根据我在 rapidjson 文档中找到的内容,您要为此使用的函数是 RawValue.