0

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

thelittlePanda
  • 101
  • 2
  • 10
  • Inside the `catch` block, try `qDebug() << ex.what();` and see why it failed. – mcchu Oct 10 '14 at 11:34
  • Yes, in ex.what() and lulu are the same strings. But the ex string should be parsed , or ? why this isn't parsed ? – thelittlePanda Oct 10 '14 at 11:51
  • Because `boost::property_tree::json_parser::read_json` cannot parse the data string in `Lulu`. Your program is correct, however the input data is wrong, maybe not a valid JSON object. – mcchu Oct 10 '14 at 12:30

3 Answers3

1

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

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

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

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.

mcchu
  • 3,309
  • 1
  • 20
  • 19
  • Okey, but finally i would parse it and give out the parsed strings this QDebugs are only for me for help. boost::property_tree::ptree propertyTree; try { boost::property_tree::json_parser::read_json(streamedJson, propertyTree); } what i should do now with my streamedJson ? – thelittlePanda Oct 10 '14 at 11:24
  • thats have a bad format, i add it in my question examplecode. – thelittlePanda Oct 10 '14 at 11:24
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 Lulu = QString(data);
std::stringstream streamedJson ;
std::string strLulu = Lulu.toStdString();
streamedJson << strLulu;
qDebug() << "streamedJson: "<< streamedJson;

Hope this helps.

Nithish
  • 1,580
  • 12
  • 21
-1

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

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:

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

I believe it will work.

gsa
  • 516
  • 5
  • 9
  • Thank you. streamedJson << Lulu.toStdString().c_str(); an streamedJson << Lulu.toStdString(); doesn't works, i have copy the false code in the example ;) now its the right. – thelittlePanda Oct 10 '14 at 08:01
  • Do you have an other idea :( ? – thelittlePanda Oct 10 '14 at 08:43
  • You can change your code in order to test, how it works. I would create an additional string, which will contain the value returned from toStdString(). Add a breakpoint at the beginning and trace your program. – gsa Oct 10 '14 at 10:24
  • i have debugg my programm .. see the comments in my bsp code. In Lulu it views the right , but in streamedJson its have the false string .. before the strings is right .. – thelittlePanda Oct 10 '14 at 10:52