Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QJSonArray to QString conversion

Tags:

c++

qstring

qt5

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.

like image 239
walolinux Avatar asked Jun 07 '15 21:06

walolinux


People also ask

How to convert QJsonArray to QString?

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.

What is QJsonArray?

QJsonArray::QJsonArray(std::initializer_list<QJsonValue> args) Creates an array initialized from args initialization list. QJsonArray can be constructed in a way similar to JSON notation, for example: QJsonArray array = { 1, 2.2, QString() }; This function was introduced in Qt 5.4.

How do you convert QJsonValue to QJsonObject?

Use toObject() to convert to a QJsonObject. The value is undefined. This is usually returned as an error condition, when trying to read an out of bounds value in an array or a non existent key in an object.


1 Answers

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!

like image 170
MrEricSir Avatar answered Sep 22 '22 21:09

MrEricSir