JasonYANG17 2023-02-20 22:12 采纳率: 93.3%
浏览 44
已结题

jsoncpp如何获取data内信息

{"success":true,"title":"百度热点","subtitle":"指数","update_time":"2023-02-20 08:56:02","data":[{"index":1,"title":"布林肯称中或向俄提供武器 中方驳斥","d......

我希望读取到data内index1的信息,似乎[符号阻止了我的访问。一下是jsoncpp代码,我要如何修改?


        CString str = content;
        Json::Reader reader;
        Json::Value root;
        CString temp;
        CString temp2;
        CString temp3;
        CString temp4;
        CString temp5;
        CString temp6;
        USES_CONVERSION;
        char* cJson = T2A(str.GetBuffer(0));
        str.ReleaseBuffer();
        if (reader.parse(cJson, root)) {
            temp2 = root["data"].asCString();

            MessageBox(temp2);
        }
  • 写回答

3条回答 默认 最新

  • 菜鸟才能学的更多 2023-02-20 22:47
    关注

    根据提供的 JSON 数据,可以看出 "data" 对应的值是一个数组,因此需要先获取数组,然后再获取数组中的某一项数据。

    可以使用 JsonCpp 库提供的 operator[] 和 get 方法来获取 JSON 数组中指定的数据项。以下是修改后的代码:

            // 解析 JSON 数据
            Json::Reader reader;
            Json::Value root;
            CString temp;
            USES_CONVERSION;
            char* cJson = T2A(content.GetBuffer(0));
            content.ReleaseBuffer();
            if (reader.parse(cJson, root)) {
                // 获取 "data" 数组
                Json::Value dataArray = root["data"];
                // 获取数组中第一项的 "index" 值
                int index = dataArray[0]["index"].asInt();
                // 获取数组中第一项的 "title" 值
                CString title = A2T(dataArray[0]["title"].asCString());
                // 显示获取到的数据
                temp.Format(_T("index: %d, title: %s"), index, title);
                MessageBox(temp);
            }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月28日
  • 已采纳回答 2月20日
  • 创建了问题 2月20日

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理