c++ - 从 JSON 字符串 C++ 读取时出现字符串下标超出范围错误
问题描述
我正在尝试创建一个返回 JSON 属性元素的函数。
string Json_obj::get_attribute(const string& attribute) {
size_t found = body_.find(attribute);
char eos = ',';
string element;
if (found == string::npos) {
cerr << "Not found attribute" << endl;
}
else { //if attribute exists
size_t found_trim = found + attribute.length() + 3; //start after first quote ' mark
size_t c = found_trim;
while (body_[c] != eos) {
element = element + body_[c];
c++;
}
}
return element;
}
例如在正文中:"mrkl_root":"1234567890","time":123,"bits":456,
get_attribute("mrkl_root")
应该返回:1234567890。
相反,我得到一个字符串下标超出范围错误。
提前致谢。
解决方案
推荐阅读
- c - 如何使用 Proteus 在 pic18f4550 上使用 MCLR?
- github - Git show 不返回提交消息
- javascript - 另一个 addEventListener 回调中的 JavaScript 嵌套 addEventListener 立即触发
- python - Matplotlib:如何在 imshow() 的颜色条上添加一条线
- node.js - 如何将对象发送到 .marko 模板并在表格中动态呈现其属性
- python - 针对动态创建的对象进行类型检查
- python - 如何查看我的程序是如何逐行执行的
- tensorflow - tensorflow 消耗的 GPU 内存是否完全等于所需的
- php - 声明类型提示时,PhpStorm 是否允许跳过 PHPDoc 标记?
- c# - 碰撞时淡出单个粒子?