Instagram
Recent Posts
Recent Comments
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

기록중

[Qt] QJsonObject, QJsonValue 본문

Study/Qt/QML

[Qt] QJsonObject, QJsonValue

코튼아 2016. 2. 17. 09:15

json 파일 읽어들이고, 다시 내보내고!


jsonfile.json

{
   "appDesc": {
      "description": "SomeDescription",
      "message": "SomeMessage"
   },
   "appName": {
      "description": "Home",
      "message": "Welcome",
      "imp":["awesome","best","good"]
   },
    "icon": {
       "default": "hello.png",
       "clicked": "World.png"
    },
    "iconOld": "oneValue.png"
}
#include 
#include 
#include 
#include 
#include 

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString val;
    QFile file;
    file.setFileName("C:\\Users\\admin\\Desktop\\QtTranning\\TestQJsonObject\\AppInfo.json");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    val = file.readAll();
    file.close();
    if(file.exists()) qDebug() << "FIND";

    QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8());
    QJsonObject root = doc.object();

    QJsonDocument doc2(root);
    QString strJson(doc2.toJson(QJsonDocument::Compact));

    qDebug() << strJson;

    if(root["icon"].isObject()){
        qDebug() << "isObejct";
    }

    return a.exec();
}

main.cpp

#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);


          QString val;
          QFile file;
          file.setFileName("/home/yoonjung/JsonArrayTest/jsonfile.json");
          file.open(QIODevice::ReadOnly | QIODevice::Text);
          val = file.readAll();
          file.close();

          qWarning() <<"1 val: " << val;
          qWarning() <<" ";

          QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
          QJsonObject sett2 = d.object();

          QJsonValue value = sett2.value(QString("appName"));
          qWarning() << "2 value: " << value;
        qWarning() <<" ";

          QJsonObject item = value.toObject();
          qWarning() << "3 QJsonObject of description: " << item;
        qWarning() <<" ";

          /* incase of string value get value and convert into string*/
          qWarning() << "4  QJsonObject[appName] of description: "<< item["description"];
        qWarning() <<" ";
          QJsonValue subobj = item["description"];
          qWarning() << "5 " << subobj.toString();
        qWarning() <<" ";
          /* incase of array get array and convert into string*/
          qWarning() << "6 QJsonObject[appName] of value: "<< item["imp"];
        qWarning() <<" ";
        QJsonArray test = item["imp"].toArray();
          qWarning() << "7 " << test[1].toString();
        qWarning() <<" ";

        //YJ
        QJsonValue iconvalues = sett2.value(QString("icon"));
        QJsonObject iconObj = iconvalues.toObject();
        QJsonValue iconDefalutValue = iconObj["default"];
        QJsonValue iconClickedValue = iconObj["clicked"];
        qWarning() <<" ";
        qWarning() <<" iconvalues: " << iconvalues;
        qWarning() <<" iconObj: " << iconObj;

        qWarning() <<" iconDefalutValue: " << iconDefalutValue.toString();
        qWarning() <<" iconClickedValue: " << iconClickedValue.toString();
        //

        //YJ
        QJsonObject root = d.object();

        qWarning() <<" ";
        qWarning() <<" root[icon] isObject: " << root["icon"].isObject() ;
        qWarning() <<" root[iconOld] isObject: " << root["iconOld"].isObject() ;

        qWarning() <<" ";
        qWarning() <<" root[icon] size : " << root["icon"].toObject().size() ;
        qWarning() <<" root[iconOld] type: " << root["iconOld"].type();

        QStringList iconPaths = root["icon"].toObject().keys();
        qWarning() <<" iconPaths: keys" << iconPaths;
        qWarning() <<" iconPaths: value "
        << root["icon"].toObject().value(root["icon"].toObject().keys().at(0)).toString();


        qWarning() <<" ";
        //QJsonObject::iterator it = root["icon"].toObject().iterator;

        qWarning() <<" @@@@@@@@@@@@@@@@@@@@@@@ ";
        qWarning() <<" ";
        qWarning() <<" ";

        QJsonObject newiconObj;
        //QString iconKey;
        QString iconPath;
        QJsonObject::iterator it;

        for( it = root["icon"].toObject().begin(); it != root["icon"].toObject().end(); it++){           
            //iconKey = it.key();
            iconPath = it.value().toString();
            if( !it.value().toString().contains("://")){
                newiconObj.insert(it.key(), "file://"+iconPath);
            }
        }




    QJsonObject obj;

    QJsonValue valuesOld = root["iconOld"].toString();

    QString str = root["iconOld"].toString();

    if(str.contains("png")){
       str = "file://" + str;
    }
    valuesOld = str;
    qWarning() <<" valuesOld######: " << valuesOld.toString();


    QJsonValue valueNew = newiconObj;

    //QJsonValue valuesNew = root["icon"].toObject();
    //obj.insert("iconNew",QJsonValue(valuesNew));
    obj.insert("iconOne",QJsonValue(valuesOld));
    obj.insert("iconTwo",QJsonValue(valueNew));
    qWarning() <<" valueNew: " << valueNew;

    qWarning() <<" END" << obj;
    return a.exec();
}


참고: http://stackoverflow.com/questions/15893040/how-to-create-read-write-json-files-in-qt5

Qt Doc: http://doc.qt.io/qt-5/qjsonobject.html

'Study > Qt/QML' 카테고리의 다른 글

QJsonParseError  (0) 2016.03.31
Comments