Qt實現含有兄弟節點解析json文件


```cpp
void PinnacleAPITool::ClearData()
{
    RemoveNode(m_APIRootNode);
}

void PinnacleAPITool::RemoveNode(APINode* node)
{
    if (node)
    {
        APINode* brotherNode = node->nextBrotherNode;

        if (node->childNode)
        {
            RemoveNode(node->childNode);
        }

        while (brotherNode)
        {
            APINode* nextBrotherNode = brotherNode->nextBrotherNode;
            RemoveNode(node->nextBrotherNode);
            brotherNode = nextBrotherNode;
        }

        delete node;
        node = nullptr;
    }
}

bool PinnacleAPITool::LoadJsonData(const QString& fileName)
{
    QFile loadFile(fileName);

    if (!loadFile.open(QIODevice::ReadOnly))
    {
        QMessageBox::warning(this, "warnning", "could't open projects json");
        //qDebug() << "could't open projects json";
        return false;
    }

    QByteArray allData = loadFile.readAll();
    loadFile.close();
    QJsonParseError json_error;
    QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));

    if (json_error.error != QJsonParseError::NoError)
    {
        QMessageBox::warning(this, "warnning", "json error!");
        //qDebug() << "json error!";
        return false;
    }

    QJsonObject rootObj = jsonDoc.object();

    if (rootObj.empty())
    {
        QMessageBox::warning(this, "warnning", "there have not contexts!");
        return false;
    }

    //先清空原有的節點
    ClearData();

    if (rootObj.contains("object"))
    {
        QJsonArray subArray = rootObj.value("object").toArray();
        APINode* brotherNode = nullptr;

        for (int i = 0; i < subArray.size(); i++)
        {
            APINode* tmpNode = new APINode();

            if (0 == i)
            {
                //說明是第一個節點
                m_APIRootNode = tmpNode;
                brotherNode = tmpNode;
            }
            else
            {
                brotherNode->nextBrotherNode = tmpNode;
                brotherNode = tmpNode;
            }

            AddChildNode(subArray[i].toObject(), tmpNode);
        }
    }

    return true;
}

void PinnacleAPITool::AddChildNode(QJsonObject& jsonObj, APINode* node)
{
    node->name = jsonObj.value("name").toString();
    node->description = jsonObj.value("description").toString();

    if (jsonObj.contains("Children"))
    {
        QJsonArray subArray = jsonObj.value("Children").toArray();
        APINode* brotherNode = nullptr;

        for (int i = 0; i < subArray.size(); i++)
        {
            APINode* tmpNode = new APINode();

            if (0 == i)
            {
                node->childNode = tmpNode;
                brotherNode = tmpNode;
            }
            else
            {
                brotherNode->nextBrotherNode = tmpNode;
                brotherNode = tmpNode;
            }

            AddChildNode(subArray[i].toObject(), tmpNode);
        }
    }
}

void PinnacleAPITool::on_importBtn_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                       tr("Open pinnacle API json"), "C:\\Users\\Public\\Documents", tr("Image Files (*.json)"));

    if (fileName.isEmpty())
    {
        return;
    }

    //加載數據
    LoadJsonData(fileName);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章