• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            Cpper
            C/C++高級工程師 Android高級軟件工程師 IT集成工程師 音頻工程師 熟悉c,c++,java,c#,py,js,asp等多種語言 程序猿
            #include <QStringList>
            #include 
            <QJsonArray>
            #include 
            <QJSonDocument>
            #include 
            <QJsonObject>
            #include 
            <QJsonValue>
            #include 
            <QFile>
            #include 
            <QDebug>
            #include 
            "test.h"

            #define STR(o) #o
            #define INSERT_ITEM(object,o) object.insert(STR(o),o);
            #define INSERT_LIST(object,o)\
                {\
                    auto array 
            = QJsonArray::fromStringList(o);\
                    
            object.insert(STR(o),array);\
                }

            struct Unit
            {
                QString result;
                QString author;
                QString description;
                QString notes;

                QStringList key;
                
            int minValue;
                QStringList items;
                QStringList attachment;

                QString toString()
                {
                    QJsonObject 
            object;
                    INSERT_ITEM(
            object,result)
                    INSERT_ITEM(
            object,author)
                    INSERT_ITEM(
            object,description)
                    INSERT_ITEM(
            object,notes)
                    INSERT_LIST(
            object,key)
                    INSERT_ITEM(
            object,minValue)
                    INSERT_LIST(
            object,items)
                    INSERT_LIST(
            object,attachment)

                    QJsonDocument doc;
                    doc.setObject(
            object);
                    
            return doc.toJson(QJsonDocument::JsonFormat::Compact);
                }

                
            bool loadFromFile(const QString& filename)
                {
                    QFile file(filename);
                    
            if(!file.open(QIODevice::ReadOnly))
                        
            return false;

                    QByteArray buffer 
            = file.readAll();
                    file.close();

                    QJsonParseError error;
                    QJsonDocument doc(QJsonDocument::fromJson(buffer,
            &error));

                    
            if(error.error != QJsonParseError::NoError)
                    {
                        qDebug() 
            << "parse json file:"<<filename<< " failed!";
                        
            return false;
                    }

                    auto 
            object = doc.object();
                    
            if(!object.contains("result"|| !object.contains("key"))
                    {
                        qDebug() 
            << "invalid json file:" << filename << " structure!";
                        
            return false;
                    }

                    result 
            = object[STR(result)].toString();
                    author 
            = object[STR(author)].toString();
                    description 
            = object[STR(description)].toString();
                    notes 
            = object[STR(notes)].toString();

                    minValue 
            = object[STR(minValue)].toInt();

                    auto fn 
            = [](const QJsonArray& array)->QStringList
                    {
                        QStringList items;
                        
            foreach(auto item,array)
                            items 
            += item.toString();
                        
            return items;
                    };

                    key 
            = fn(object[STR(key)].toArray());
                    items 
            = fn(object[STR(items)].toArray());
                    attachment 
            = fn(object[STR(attachment)].toArray());
                    
            return true;
                }
            };
             
            void test()
            {
                Unit unit;
                unit.attachment.append(
            "acchemtn1");
                unit.attachment.append(
            "acchemtn2");

                unit.key.append(
            "key1");
                unit.author 
            = "author";
                unit.description 
            = "this is a remark";
                unit.items.append(
            "item1");
                unit.items.append(
            "item2");
                unit.items.append(
            "item3");
                unit.minValue 
            = 1;
                unit.notes 
            = "notes";

                QFile file(
            "file.json");
                file.open(QIODevice::WriteOnly);

                file.write(unit.toString().toLocal8Bit().data());

                file.close();

                qDebug() 
            << unit.toString();
            }
            posted on 2019-08-13 14:43 ccsdu2009 閱讀(888) 評論(0)  編輯 收藏 引用 所屬分類: QT編程
             
            97久久婷婷五月综合色d啪蜜芽| 亚洲国产精品无码久久久蜜芽| 国产精品久久精品| 青青青国产成人久久111网站| 国产三级观看久久| 东方aⅴ免费观看久久av| 精品免费tv久久久久久久| 日韩美女18网站久久精品| 久久久久久久97| 色婷婷久久久SWAG精品| 狠狠88综合久久久久综合网| 亚洲色欲久久久久综合网| 国产精品女同久久久久电影院| 日韩欧美亚洲国产精品字幕久久久| 久久亚洲AV成人无码电影| 久久久精品视频免费观看| 久久久亚洲欧洲日产国码aⅴ| 久久天天日天天操综合伊人av| 精品久久久久久无码专区不卡| 欧美亚洲国产精品久久| 久久最新精品国产| 国产成人精品久久免费动漫| 久久精品aⅴ无码中文字字幕不卡| 99久久国产综合精品五月天喷水 | 国产精品久久久久久久app | 久久久久无码精品| 久久免费视频观看| 国内精品久久久久影院优| 人妻无码中文久久久久专区| 久久99精品久久久大学生| 久久久久亚洲AV成人网人人网站| 亚洲伊人久久综合影院| 综合久久精品色| 久久人人青草97香蕉| 久久婷婷色香五月综合激情| 中文字幕精品久久| 国产精品亚洲综合久久| 国产色综合久久无码有码| 亚洲愉拍99热成人精品热久久| 一本色道久久HEZYO无码| 精品久久久中文字幕人妻|