文档章节

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

o
 osc_1ee7cxmx
发布于 2018/08/06 17:09
字数 312
阅读 7
收藏 0

精选30+云产品,助力企业轻松上云!>>>

 

一个递归搞定

无聊的时候练练手就写了一个 头文件什么的我就不贴了 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;
}

效果图

 

 

o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。

暂无文章

如果你失明了,你怎么编程? - How can you program if you're blind?

问题: Sight is one of the senses most programmers take for granted. 视觉是大多数程序员认为理所当然的感官之一。 Most programmers would spend hours looking at a computer monitor......

技术盛宴
14分钟前
10
0
如何删除使用Python的easy_install安装的软件包? - How do I remove packages installed with Python's easy_install?

问题: Python's easy_install makes installing new packages extremely convenient. Python的easy_install使安装新包非常方便。 However, as far as I can tell, it doesn't implement th......

fyin1314
44分钟前
8
0
如何将逗号分隔的字符串转换为数组? - How to convert a comma separated string to an array?

问题: I have a comma separated string that I want to convert into an array, so I can loop through it. 我有一个逗号分隔的字符串,我想将其转换成数组,因此可以循环遍历它。 Is the...

富含淀粉
今天
13
0
深源恒际:担心个人身份被冒用?不存在!

本文作者:c****t 近日,苟晶被冒名顶替身份参加高考的事件在社会各界掀起广泛热议。事件调查结果公布后,舆论风向逆转,吃瓜群众认为当事人夸大其词消费了公众情绪,一边对当事人所遭遇的不...

百度开发者中心
昨天
5
0
CKEditor 5 + SpringBoot实战(三):SpringData JPA数据持久化

在本系列的文章中,我将介绍如何在Spring Boot Application中使用CKEditor编辑器。介绍的内容包括基本环境的搭建,文件上传,SpringData JPA数据持久化,CKEditor5的安装,CKEditor图片上传,...

树下魅狐
今天
9
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部