QByteArray to QString to std::stringstream 不起作用
QByteArray to QString to std::stringstream doesn't work
任何人都知道的原因
QString Lulu ( data ); //data is a QByteArry ( from a QNetworkrequest )
std::stringstream streamedJson ;
// QString Lulu ( data.data() );
qDebug()<< "Lulu:" << Lulu; // here it views the right string
streamedJson << Lulu.toStdString();
qDebug() << "streamedJson: "<< streamedJson ; // here it views 0x7fffc9d46568
不起作用?为什么不在这里查看字符串?最后,我会解析它,并给出解析后的字符串
boost::property_tree::ptree propertyTree;
try
{
boost::property_tree::json_parser::read_json(streamedJson, propertyTree);
}
catch(boost::property_tree::json_parser::json_parser_error& ex)
{
qDebug() << "ex: "<< ex.what(); // this and Lulu views the same (unparsed) string
qDebug ("propertyree error");
}
目前,它只查看"属性错误"。但它应该在我的控制台中打印解析后的字符串
std::stringstream
不能直接与QDebug::operator<<
一起使用。您可以显式地将其转换为QString
。例如,
qDebug() << "streamedJson: " << QString::fromStdString(streamedJson.str());
streamedJson.str()
返回std::string
,然后使用QString::fromStdString
转换为QString
。
您的程序打印0x7fffc9d46568
可能是因为streamedJson
被隐式转换为qDebug
可打印对象。或者,程序中的某个地方可能有一个operator<<
函数,它将std::stringstream
作为输入。
尝试初始化QString
变量,如下所述,并尝试将值放入std::string
变量,然后再将其推入std::stringstream
。
QString Lulu = QString(data);
std::stringstream streamedJson ;
std::string strLulu = Lulu.toStdString();
streamedJson << strLulu;
qDebug() << "streamedJson: "<< streamedJson;
希望这能有所帮助。
类QString
具有函数std::string toStdString() const
。也许你应该这样使用它:
streamedJson << Lulu.toStdString();
如果它不起作用,你可以尝试
streamedJson << Lulu.toStdString().c_str();
如果它也不起作用,我们将找到另一个可能的解决方案。祝你好运
编辑:我看了好几份文件,我想我已经解决了你的问题。类std::stringstream
具有字符串的内部表示。为了从这个类中获得std::string
,您应该使用它的函数str()
:http://www.cplusplus.com/reference/sstream/stringstream/str/那么你的代码应该是这样的:
string myString = streamedJson.str();
std::cout << myString;
我相信它会起作用的。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- C++ Singleton - Prevent ::instance() to variable
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- Python str to C++ to Python str
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- "no matching function for call to 'Vector::Vector'"错误
- vector<vector<double>> to mxArray using memcpy
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- QByteArray to QString to std::stringstream 不起作用
- c++ stringstream to ostream to string
- C++ stringstream string to function(test, char *test2)