当前位置:网站首页>JSON parsing instance (QT including source code)

JSON parsing instance (QT including source code)

2022-07-07 14:49:00 51CTO

JSON analysis

Qt There is a built-in QJsonDocument Class is used to handle json file , as well as json character string . Analytical thinking first string Load into QJsonDocument in , Then analyze it layer by layer .json The format of contains two ,1) object ,2) Array

name analysis
QJsonDocument A complete package JSON Class of document , It can be downloaded from UTF-8 Coded text-based representation and Qt Read and write the document in its own binary format
QJsonObject Is a list of key value pairs , Where the key is the only string , Values are determined by QJsonValue Express .
QJsonArrayJSON An array is a list of values . You can insert and delete... From the array QJsonValue To manipulate the list .
QJsonValueJSON It's a format for storing structured data . It has 6 Basic data types :bool、double、string、array、object、null
QJsonParseError Used in JSON Error reported during parsing

Object parsing

Test data

{"Name":" Xiao Ming ","age":18}

     
  • 1.

Qt json Parse object   Code

void MainWindow::parseObject()
{
    QString str = "{\"Name\":\" Xiao Ming \",\"Age\":18}";
    QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8());
    QJsonObject jobject = jdoc.object();
    QJsonValue jvalue_name = jobject.value("Name");
    qInfo() << jvalue_name.toString();
    QJsonValue jvalue_age = jobject.value("Age");
    qInfo() << jvalue_age.toInt();

    /////////  Traverse  /////////////
    foreach (auto item, jobject.keys()) {
        qInfo() << "key:" << item;
        QJsonValue jvalue = jobject.value(item);
        qInfo() << "value:" << jvalue.toVariant();
    }
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

Running results

" Xiao Ming "
18
key: "Age"
value: QVariant(double, 18)
key: "Name"
value: QVariant(QString, " Xiao Ming ")

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

Array parsing

Test data

["apple","orange","banana","watermelon"]

     
  • 1.

Qt json Parse array code

void MainWindow::parseArray()
{
    QString str = "[\"apple\",\"orange\",\"banana\",\"watermelon\"]";
    QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8());
    QJsonArray jArray = jdoc.array();
    // item  by QJsonValue type 
    foreach (auto item, jArray) {
        qInfo() << item.toString();
    }
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

Running results

"apple"
"orange"
"banana"
"watermelon"

     
  • 1.
  • 2.
  • 3.
  • 4.

Mixed parsing of array objects

Test data

{"Lists":["apple","orange"]}

     
  • 1.

Qt json Parsing code

void MainWindow::parseObjectArray()
{
    QString str = "{\"Lists\":[\"apple\",\"orange\"]}";
    QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8());
    QJsonObject jObject = jdoc.object();
    QJsonValue jLists = jObject.value("Lists");
    QJsonArray jArray = jLists.toArray();
    // item  by QJsonValue type 
    foreach (auto item, jArray) {
        qInfo() << "value:" << item.toString();
    }
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

Running results

value: "apple"
value: "orange"

     
  • 1.
  • 2.

Object array mixed parsing

Test data

[{"Name":" Xiao Ming ","age":18},{"Name":" Xiao Wang ","age":20}]

     
  • 1.

Qt analysis json Code

void MainWindow::parseArrayObject()
{
    QString str =
        "[{\"Name\":\" Xiao Ming \",\"age\":18},{\"Name\":\" Xiao Wang \",\"age\":20}]";
    QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8());
    QJsonArray jLists = jdoc.array();
    foreach (auto item, jLists) {
        QJsonObject item_object = item.toObject();
        qInfo() << "===============================";
        qInfo() << "name:" << item_object.value("Name").toString();
        qInfo() << "age:" << item_object.value("age").toDouble();
    }
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

Running results

===============================
name: " Xiao Ming "
age: 18
===============================
name: " Xiao Wang "
age: 20

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

official account :【qt The little prince 】
JSON Resolving instances (Qt Including source code )_json

原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071233106173.html