我有这个C ++代码,并且在json序列化时遇到了麻烦。
string uInput;
string const& retInput;
while(!std::cin.eof()) {
getline(cin, uInput);
JSONExample source; //JSON enabled class from jsonserialize.h
source.text = uInput;
//create JSON from producer
std::string json = JSON::producer<JSONExample>::convert(source); //string -> returns {"JSONExample":{"text":"hi"}}
//then create new instance from a consumer...
JSONExample sink = JSON::consumer<JSONExample>::convert(json);
//retInput = serialize(sink);
// Json::FastWriter fastWriter;
// retInput = fastWriter.write(uInput);
retInput = static_cast<string const&>(uInput);
pubnub::futres fr_2 = pb_2.publish(chan, retInput);
cout << "user input as json which should be published is " << retInput<< std::endl;
答案 0 :(得分:3)
while(!cin.eof()) {
getline(cin, uInput);
newInput = "\"\\\"";
newInput += uInput;
newInput += "\\\"\"";
此代码不是输入“\”hi \“”这样的消息,而是采用“hi”并执行此操作。
答案 1 :(得分:1)
如果您所描述的更改成为&#34;无效的JSON&#34;消失,然后更正确&#34;解决方案是AFAICT,将publish()
行更改为:
pubnub::futres fr_2 = pb_2.publish(chan, json);
因为json
已经有JSON序列化数据。当然,如果你想要发布那个JSON。