PDA

View Full Version : Write JSON array in one line



abshaev
4th March 2024, 18:46
Hello All!

I want to write boolean array in one line of JSON file., like here
"a": [false, true, true, false] or "a": [0, 1, 1, 0]


I do like this:

...
typedef bool my_arr[4];
...

QJsonArray json_bool_arr{my_arr[0], my_arr[1], my_arr[2], my_arr[3]};
json_book.insert("my_bool_array", json_bool_arr);



but in output JSON file I got 4 separate lines with values :

"my_bool_array": [
false,
true,
true,
false
],

How can I solve this issue ?

Ginsengelf
5th March 2024, 07:32
Hi, you could try to replace the line feeds before writing the data to the file. If you use the toJson() methode, check QByteArray::replace().

By the way, your typedef line looks weird in combination with your json_bool_arr variable. Are you sure that you want/need a typedef there?

Ginsengelf