[solved]Problems parsing a .json (resulting QJsonDocument always empty/Null)
-
Good Morning Sgaist,
the txtJsonFile widgeht gets its content within the program window (when the user specifies the file to open). I proofed the widget`s content via qDebug(), the processing of the entered file name is correct. Setting the filename directly within the code leads to the same result. If the code given above is correct, are there any other possible reasons for the parser to fail? A check with an HexEditor shows that all files I tried start with an "{" (Hex7B) and the encoding is UTF8, as it should be. There is also no BOM, so I am really confused!
-
What version of Qt are you using? Did you use an official installer or build it yourself?
Please modify your program as follows and post the output:
@
tmp.open(QIODevice::ReadOnly | QIODevice::Text);
QByteArray bytes = tmp.readAll();
qDebug() << bytes;mJsonFile = QJsonDocument::fromJson(bytes,error);
qDebug() << mJsonFile;
qDebug() << error->errorString();
@ -
My Qt-Version is 5.4.0 (Creator 3.30), with MinGW, and I used the official installer. The output you requested is as follows:
@
"{
"version": "1.0",
"gate": {
"id": 29
},
"data": [
{
"address": 410,
"group": 0,
"type": "11:0",
"timeStamp": "2015-01-15 10:05:11.000",
"values": [
100,
4.2,
0
]
},
{
"address": 410,
"group": 1,
"type": "11:1",
"timeStamp": "2015-01-15 10:05:11.000",
"values": [
-76,
0.14,
256,
0,
12,
866,
46241,
0
]
},
{
"address": 410,
"group": 2,
"type": "11:2",
"timeStamp": "2015-01-15 10:05:11.000",
"values": [
22.1312312,
50.1231313,
30.4,
0,
6,
1,
1.42,
1.04,
0.98
]
},
{
"address": 411,
"group": 0,
"type": "26:0",
"timeStamp": "2015-01-15 10:05:11.000",
"values": [
100,
4.2,
0,
20
]
},
{
"address": 411,
"group": 1,
"type": "26:1",
"timeStamp": "2015-01-15 10:05:11.000",
"values": [
22.1312312,
50.1231313,
30.4,
0,
6,
1,
1.42,
1.04,
0.98,
12
]
},
{
"address": 411,
"group": 2,
"type": "26:2",
"timeStamp": "2015-01-15 10:05:11.000",
"values": [
827850752,
73847942565529090,
827850752,
73847942565529090,
827850752,
73847942566053380,
827850752,
73847942566053380,
827850752,
73847942566577660,
25
]
}
]
}"
QJsonDocument()
"illegal value"
@ -
I also used the official Qt 5.4.0 + MinGW, on Windows 8.1. I got...
@
QJsonDocument({"data":[{"address":410,"group":0,"timeStamp":"2015-01-15 10:05:11.000","type":"11:0","values":[100,4.2000000000000002,0]},{"address":410,"group":1,"timeStamp":"2015-01-15 10:05:11.000","type":"11:1","values":[-76,0.14000000000000001,256,0,12,866,46241,0]},{"address":410,"group":2,"timeStamp":"2015-01-15 10:05:11.000","type":"11:2","values":[22.131231199999998,50.123131299999997,30.399999999999999,0,6,1,1.4199999999999999,1.04,0.97999999999999998]},{"address":411,"group":0,"timeStamp":"2015-01-15 10:05:11.000","type":"26:0","values":[100,4.2000000000000002,0,20]},{"address":411,"group":1,"timeStamp":"2015-01-15 10:05:11.000","type":"26:1","values":[22.131231199999998,50.123131299999997,30.399999999999999,0,6,1,1.4199999999999999,1.04,0.97999999999999998,12]},{"address":411,"group":2,"timeStamp":"2015-01-15 10:05:11.000","type":"26:2","values":[827850752,73847942565529088,827850752,73847942565529088,827850752,73847942566053376,827850752,73847942566053376,827850752,73847942566577664,25]}],"gate":{"id":29},"version":"1.0"})
"no error occurred"
@...so something strange is going on with your machine. I've never seen this problem before.
Could you try on a different PC?
-
Thats really strange! As I said above, I tried the same code on two machines; a macbook and a windows pc. The result was the same... I still have no Idea, what could be the reason.
-
Then let's do some shots in the dark: Country ? File Encoding ? Codepage ?
-
[quote author="SGaist" date="1423948310"]Then let's do some shots in the dark: Country ? File Encoding ? Codepage ? [/quote]Good idea. Try setting your computer's Locale to an English-speaking country (e.g. USA or UK), reboot your PC, and see if the program works now. (Double-check that the file really is encoded in UTF-8)
-
I solved the problem this morning, although I am still a little bit confused. It seems that my debug output sets the file pointer to the end of the file, so that the parser considers it to be empty. That makes sense, but I had the same mistake before I inserted the code for the output. And, finally, the reason to have the output was the parser error.... nevertheless, thank you very much for your help and your patience!
-
I'm a bit confused.
Earlier, you said that the following code outputted the full document (which means your file pointer was ok)...
@
QByteArray bytes = tmp.readAll();
qDebug() << bytes;
@...but the following code produces emptiness, even though bytes already contains the full document?
@
mJsonFile = QJsonDocument::fromJson(bytes,error);
qDebug() << mJsonFile;
@ -
Unfortunately, I was stupid enough to set another output directly after the file is opened. Something like this:
@
...
if (f.open(QIODevice::ReadOnly)) {
qDebug() << "File opened";
qDebug() << f.readAll(); //Stupid!
QByteArray data = f.readAll();
QJsonParseError error;
QJsonDocument json(QJsonDocument::fromJson(data));
qDebug() << json; // Thats ok!
qDebug() << error.error << error.errorString();
qDebug() << json.object();
qDebug() << json.isNull() << json.isEmpty();
} else {
qDebug() << "could not open File!";
}
} else {
qDebug() << "Error file does not exist";
...
@ -
Yeah, calling readAll() sets the pointer at the end of the file. You can't read it twice without resetting the QFile.
But anyway, it's good that you've found the solution :) Happy coding!
P.S. In the future, please post the code that you actually used, and please try the sample code that we post for you without changing it (unless the sample is wrong). That will prevent confusions like this in the future.
-
It was not my intention to confuse you, but my code changed over the days as I continued the search for the solution of my problem. Therefore, the samples here were not up to date from time to time. Sorry again!
-
It's ok :)
[quote author="fharjes" date="1424158467"]Therefore, the samples here were not up to date from time to time.[/quote]I meant my samples -- The output that you posted on 13 Feb was obtained using different code from what I posted.
So what I'm trying to say is: When others give you code in order to check output, run that code unmodified. Otherwise, your output might be wrong.