How to create/read/write JSON files in Qt5

后端 未结 3 1688
予麋鹿
予麋鹿 2020-12-02 07:18

Qt5 has a new JSON parser and I want to use it. The problem is that it isn\'t too clear about what the functions do in layman\'s terms and how to write code with it. That or

3条回答
  •  有刺的猬
    2020-12-02 07:39

    Example: Read json from file

    /* test.json */
    {
       "appDesc": {
          "description": "SomeDescription",
          "message": "SomeMessage"
       },
       "appName": {
          "description": "Home",
          "message": "Welcome",
          "imp":["awesome","best","good"]
       }
    }
    
    
    void readJson()
       {
          QString val;
          QFile file;
          file.setFileName("test.json");
          file.open(QIODevice::ReadOnly | QIODevice::Text);
          val = file.readAll();
          file.close();
          qWarning() << val;
          QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
          QJsonObject sett2 = d.object();
          QJsonValue value = sett2.value(QString("appName"));
          qWarning() << value;
          QJsonObject item = value.toObject();
          qWarning() << tr("QJsonObject of description: ") << item;
    
          /* in case of string value get value and convert into string*/
          qWarning() << tr("QJsonObject[appName] of description: ") << item["description"];
          QJsonValue subobj = item["description"];
          qWarning() << subobj.toString();
    
          /* in case of array get array and convert into string*/
          qWarning() << tr("QJsonObject[appName] of value: ") << item["imp"];
          QJsonArray test = item["imp"].toArray();
          qWarning() << test[1].toString();
       }
    

    OUTPUT

    QJsonValue(object, QJsonObject({"description": "Home","imp": ["awesome","best","good"],"message": "YouTube"}) ) 
    "QJsonObject of description: " QJsonObject({"description": "Home","imp": ["awesome","best","good"],"message": "YouTube"}) 
    "QJsonObject[appName] of description: " QJsonValue(string, "Home") 
    "Home" 
    "QJsonObject[appName] of value: " QJsonValue(array, QJsonArray(["awesome","best","good"]) ) 
    "best" 
    

    Example: Read json from string

    Assign json to string as below and use the readJson() function shown before:

    val =   
    '  {
           "appDesc": {
              "description": "SomeDescription",
              "message": "SomeMessage"
           },
           "appName": {
              "description": "Home",
              "message": "Welcome",
              "imp":["awesome","best","good"]
           }
        }';
    

    OUTPUT

    QJsonValue(object, QJsonObject({"description": "Home","imp": ["awesome","best","good"],"message": "YouTube"}) ) 
    "QJsonObject of description: " QJsonObject({"description": "Home","imp": ["awesome","best","good"],"message": "YouTube"}) 
    "QJsonObject[appName] of description: " QJsonValue(string, "Home") 
    "Home" 
    "QJsonObject[appName] of value: " QJsonValue(array, QJsonArray(["awesome","best","good"]) ) 
    "best" 
    

提交回复
热议问题