When reading QJsonArray it gives always 0
-
It's etchiness depends on particular case. In some applications, JSON is used as an internal format, where rules can be different than official ones. And being able to specify a number by either string or number is useful.
Plus, let's not forget JSON does not support integers at all. So if you want to specify an integer and be sure it won't be fuzzy, using a string is OK.
-
I don't know where @suslucoder gets his JSON from. And I admit in my code I save/restore integers (C++ type) not floating point. But for integers at least I note that Qt JSON saves as a number, not as a string. So I imagine it would save a floating point similarly. Just saying.
-
@sierdzio I did it like this way. But it cant read some datas still. What should be the reason? It reads the first and last element of 1 array. And then, didnt read the second item of an array. Read the 4.element of array 2. I didnt understand
-
Since you created the json manually you're mixing
.
and,
. Use the proper json notation for doubles without quotes if you want that someone else will be able to read them and don't create such files manually. -
@Christian-Ehrlicher I have LineEdit. I take datas from line edit on my qui, and save those datas in txt file which is formatted json. I want to take float or double nums on line edit.
I have " " because line edit accept strings.
-
@suslucoder said in When reading QJsonArray it gives always 0:
I have " " because line edit accept strings.
But you cant save strings in the same way as you save floats / doubles
or ints(edit: No ints).@Christian-Ehrlicher said in When reading QJsonArray it gives always 0:
It's not 5.0 but "5.0" and this is a string. See also QJsonValue::Type()
That's what @Christian-Ehrlicher said here
So you have to differentiate what is going to be stored in your Json.
(If you build your Json yourself -> strings with"....."
and floats with.
as separator to avoid read errors)You can save everything as string, but then you have to convert it when reading the values again (-> also works best when using a dot as separator. Not every locale supports
,
as decimal separator. This is why you probably got0
) -
@suslucoder
...Which is why I suggested you might choose to save/put in file41.123
instead of string with quotes like your"41,123"
, then you will be storing a number and won't have to convert from a string.... -
Or use a proper QDoubleSpinbox
-
Or a
QLineEdit
with an input mask, so you can be sure that is input of this widget gonna be a number. Then you can write it as double to your Json. -
@suslucoder said in When reading QJsonArray it gives always 0:
ı didnt get my json from anywhere. I create it by myself
So this is not a widget issue, at least at this stage. It's a question of OP deciding on a type to be used in the JSON.
-
Exactly. You should format the data properly when you save it. Then the whole problem will just go away and a plain
toDouble()
will work when reading the file. -
Thanks for all answers. I will reformat my json file and try it again