12

I have this 2 variables, and I want to convert data to dataToString.

QJSonArray data;

Qstring dataToString;

In data there is a huge json like:

{
    "properties": [
        {
            "version":"1",
            "finish":"0",
            "num":3,
            "running":false,
            "time":"00:20:00",
            "player1":"John",
            "player2":"",
            "player3": "Peter",
            "player4":"",
            "team1":"",
            "team2":"",
            "tournament":"",
            "lap":""
        }
    ],
    "game": [
        {
            "serve":true,
            "score":"32",
            "data":"0"
        }
    ]
}

How can I do it ? Thanks.

p.i.g.
  • 2,815
  • 2
  • 24
  • 41
walolinux
  • 531
  • 1
  • 6
  • 20

1 Answers1

24

To quote the documentation:

You can convert the array to and from text based JSON through QJsonDocument.

In other words, all you need to do is this:

QJsonArray data;
QJsonDocument doc;
doc.setArray(data);

QString dataToString(doc.toJson());

That's all there is to it!

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
MrEricSir
  • 8,044
  • 4
  • 30
  • 35