QByteArray to QString to std::stringstream不起作用。

时间:2022-08-10 19:39:14

anyone know why

谁知道为什么

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

doesn't works ? why it dont view the String here ? finally i would parse it and give the parsed string out

不工作?为什么这里不查看字符串?最后,我将解析它并给出解析的字符串。

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");
       }

at the moment it only views "propertyree error". but it should print the parsed String in my console

目前它只看到“propertyree error”。但是它应该在我的控制台上打印出解析过的字符串。

3 个解决方案

#1


0  

Try initialization of the QString variable as explained below and try putting the value into a std::string variable before pushing it into the std::stringstream.

尝试初始化QString变量,并尝试将该值放入std::string变量,然后将其推入std::stringstream。

QString Lulu = QString(data);
std::stringstream streamedJson ;
std::string strLulu = Lulu.toStdString();
streamedJson << strLulu;
qDebug() << "streamedJson: "<< streamedJson;

Hope this helps.

希望这个有帮助。

#2


0  

std::stringstream cannot be directly used with QDebug::operator<<. You can explicitly convert it to QString. For example,

std::stringstream不能直接用于QDebug::操作符<<。您可以显式地将其转换为QString。例如,

qDebug() << "streamedJson: " << QString::fromStdString(streamedJson.str());

The streamedJson.str() returns an std::string, and then converted to QString using QString::fromStdString.

str()返回一个std::string,然后使用QString::fromStdString转换为QString。

Your program prints 0x7fffc9d46568 probably because the streamedJson is converted to a qDebug-printable object implicitly. Or maybe there is an operator<< function somewhere in your program that takes std::stringstream as input.

您的程序将打印0x7fffc9d46568,这可能是因为streamedJson被隐式地转换为qDebug-printable对象。或者,在您的程序中有一个操作符< <函数,它以std::stringstream作为输入。< p>

#3


-1  

Class QString has function std::string toStdString() const. Maybe you should use it this way:

类QString具有函数std::string toStdString() const。也许你应该这样使用:

streamedJson << Lulu.toStdString();

If it will not work you can try

如果不行,你可以试一试。

streamedJson << Lulu.toStdString().c_str();

If it will not work too we would find another possible solution. Good luck!

如果它不奏效,我们会找到另一个可能的解决办法。好运!

EDIT: I've read several documents and I guess I've solved your problem. Class std::stringstream has inner representation of string. In order to get std::string from this class you should use its function str(): http://www.cplusplus.com/reference/sstream/stringstream/str/ Then your code should be looking like this:

编辑:我看了几个文件,我想我已经解决了你的问题。类std::stringstream具有字符串的内部表示形式。为了获得std::这个类的字符串,您应该使用它的函数str(): http://www.cplusplus.com/reference/sstream/stringstream/str/,那么您的代码应该是这样的:

string myString = streamedJson.str();
std::cout << myString;

I believe it will work.

我相信这行得通。

#1


0  

Try initialization of the QString variable as explained below and try putting the value into a std::string variable before pushing it into the std::stringstream.

尝试初始化QString变量,并尝试将该值放入std::string变量,然后将其推入std::stringstream。

QString Lulu = QString(data);
std::stringstream streamedJson ;
std::string strLulu = Lulu.toStdString();
streamedJson << strLulu;
qDebug() << "streamedJson: "<< streamedJson;

Hope this helps.

希望这个有帮助。

#2


0  

std::stringstream cannot be directly used with QDebug::operator<<. You can explicitly convert it to QString. For example,

std::stringstream不能直接用于QDebug::操作符<<。您可以显式地将其转换为QString。例如,

qDebug() << "streamedJson: " << QString::fromStdString(streamedJson.str());

The streamedJson.str() returns an std::string, and then converted to QString using QString::fromStdString.

str()返回一个std::string,然后使用QString::fromStdString转换为QString。

Your program prints 0x7fffc9d46568 probably because the streamedJson is converted to a qDebug-printable object implicitly. Or maybe there is an operator<< function somewhere in your program that takes std::stringstream as input.

您的程序将打印0x7fffc9d46568,这可能是因为streamedJson被隐式地转换为qDebug-printable对象。或者,在您的程序中有一个操作符< <函数,它以std::stringstream作为输入。< p>

#3


-1  

Class QString has function std::string toStdString() const. Maybe you should use it this way:

类QString具有函数std::string toStdString() const。也许你应该这样使用:

streamedJson << Lulu.toStdString();

If it will not work you can try

如果不行,你可以试一试。

streamedJson << Lulu.toStdString().c_str();

If it will not work too we would find another possible solution. Good luck!

如果它不奏效,我们会找到另一个可能的解决办法。好运!

EDIT: I've read several documents and I guess I've solved your problem. Class std::stringstream has inner representation of string. In order to get std::string from this class you should use its function str(): http://www.cplusplus.com/reference/sstream/stringstream/str/ Then your code should be looking like this:

编辑:我看了几个文件,我想我已经解决了你的问题。类std::stringstream具有字符串的内部表示形式。为了获得std::这个类的字符串,您应该使用它的函数str(): http://www.cplusplus.com/reference/sstream/stringstream/str/,那么您的代码应该是这样的:

string myString = streamedJson.str();
std::cout << myString;

I believe it will work.

我相信这行得通。