1

I have this Json file:

{
  "place": {
  "id": "99"
  },
  "camera": {
  "id": "99",
  "url": "http://192.168.0.250/stuff.php?",
  }
}

What i get is all the content printed in one line with character \n printed after each field and not as it is in the file. This happens on my Ubuntu machine, but not in another machine with Debian. I'd like the file content printed line by line.. any hints? I use this code to print the content of a Json file:

QString val;
QFile file;
file.setFileName("../config.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
val = file.readAll();
file.close();
qWarning() << val;

EDIT

Tried both solution provided by user TheDarkKnight with no luck, I also tried with cat command in terminal and the file is printed correctly, so I suspect is not a matter of OS, but Qt is involved..

rok
  • 2,574
  • 3
  • 23
  • 44

2 Answers2

1

Seems that qWarning() function escapes non-printable characters and add new line character at the end, so the solution is to use:

qWarning().noquote()

https://stackoverflow.com/a/39561345/4267439

This however happen from Qt 5.4 which changed qWarning behaviour, so my final solution was this preprocessor directive:

 #if (QT_VERSION > QT_VERSION_CHECK(5, 4, 0))
   qWarning().noquote() << val;
 #else
   qWarning() << val;
 #endif
Community
  • 1
  • 1
rok
  • 2,574
  • 3
  • 23
  • 44
0

Format the data with QJsonDocument and export it from there

//Assuming data is in QString val

QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8());
QString formatted = doc.toJson(QJsonDocument::Indented);

qDebug() << formatted;
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • please could you tell me in your opinion why this is happening only in some linux distros? – rok Sep 13 '16 at 14:39
  • I can't tell you definitively without researching it, but perhaps it may be that the distros handle carriage return and line feeds differently. Did you create the original on one distro and copy to the other? If so, you could try creating it on each machine and see if that makes a difference. – TheDarkKnight Sep 13 '16 at 15:11
  • I tried your solutions, but it doesn't work for me.. it still prints inline with \n carachter and in addition prints it in the wrong order, `camera` field is printed before `place` field.. – rok Sep 22 '16 at 16:42
  • Not sure why that doesn't work for you, but there's no such thing as the 'wrong order' in Json. Qt just happens to alphabetise the keys of objects. – TheDarkKnight Sep 26 '16 at 08:04