使用rapidjson把文本json数据解析到树状结构

 

一个递归搞定

无聊的时候练练手就写了一个 头文件什么的我就不贴了 demo程序是MFC写的

void ParseObject(rapidjson::Value dc, CTreeCtrl * pTreeRoot,HTREEITEM hTreeHandle)
{
    for (rapidjson::Value::ConstMemberIterator it = dc.MemberBegin(); it != dc.MemberEnd(); it++)
    {
        std::wstring strName = Ansi_To_Unicode(it->name.GetString());
        HTREEITEM pTmpObj = NULL;
        if (dc[it->name.GetString()].IsObject() || dc[it->name.GetString()].IsArray())
        {
            pTmpObj = pTreeRoot->InsertItem(strName.c_str(), hTreeHandle);
        }
        else
        {
            strName += _T(":");
            
            if (it->value.IsInt64())
            {
                strName += std::to_wstring(it->value.GetInt64());
                pTmpObj = pTreeRoot->InsertItem(strName.c_str(), hTreeHandle);
            }
            else if (it->value.IsDouble())
            {
                strName += std::to_wstring(it->value.GetDouble());
                pTmpObj = pTreeRoot->InsertItem(strName.c_str(), hTreeHandle);
            }
            else if (it->value.IsBool())
            {
                strName += std::to_wstring((int)it->value.GetBool());
                pTmpObj = pTreeRoot->InsertItem(strName.c_str(), hTreeHandle);
            }
            else if (it->value.IsFloat())
            {
                strName += std::to_wstring(it->value.GetFloat());
                pTmpObj = pTreeRoot->InsertItem(strName.c_str(), hTreeHandle);
            }
            else if (it->value.IsInt())
            {
                strName += std::to_wstring(it->value.GetInt());
                pTmpObj = pTreeRoot->InsertItem(strName.c_str(), hTreeHandle);
            }
            else if (it->value.IsNumber())
            {
                strName += std::to_wstring(it->value.GetUint());
                pTmpObj = pTreeRoot->InsertItem(strName.c_str(), hTreeHandle);
            }
            else if (it->value.IsNull())
            {
                strName += _T("");
                pTmpObj = pTreeRoot->InsertItem(strName.c_str(), hTreeHandle);
            }
            else if (it->value.IsString())
            {
                strName += Ansi_To_Unicode(it->value.GetString());
                pTmpObj = pTreeRoot->InsertItem(strName.c_str(), hTreeHandle);
            }
        }

        if (dc[it->name.GetString()].IsObject() && pTmpObj)
        {
            ParseObject(dc[it->name.GetString()].GetObjectW(), pTreeRoot, pTmpObj);
        }
        if (dc[it->name.GetString()].IsArray() && pTmpObj)
        {
            int index = 0;
            for (auto &v : dc[it->name.GetString()].GetArray())
            {
                if (v.IsObject())
                {
                    HTREEITEM pTmparr = pTreeRoot->InsertItem((strName + _T("[") + std::to_wstring(index) + _T("]")).c_str(), pTmpObj);
                    if (pTmparr)
                        ParseObject(v.GetObjectW(), pTreeRoot, pTmparr);
                }
                index++;
            }
        }
    }
}
void CtreeDemoDlg::OnBnClickedButton1()
{
    CTreeCtrl * pTreeRoot = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
    if (!pTreeRoot)
        return;
    std::string strResult;
    CString buftext;
    GetDlgItemText(IDC_RICHEDIT21, buftext);
    strResult = Unicode_To_Ansi(buftext.GetBuffer());
    if (strResult.empty())return;
    //wstring strbuffer = Ansi_To_Unicode(strResult.c_str());
    rapidjson::Document dc;
    dc.Parse(strResult.c_str());
    if (!dc.IsObject())
        return;
    HTREEITEM pTreeObj =  pTreeRoot->InsertItem(_T("object"));
    if (!pTreeObj)
        return;
    HTREEITEM pTmpObj = nullptr;
    ParseObject(dc.GetObjectW(), pTreeRoot, pTreeObj);
    return;
}

效果图

使用rapidjson把文本json数据解析到树状结构_第1张图片

 

使用rapidjson把文本json数据解析到树状结构_第2张图片

 

你可能感兴趣的:(使用rapidjson把文本json数据解析到树状结构)