更改 json 值对使用提升C++没有影响
Changing json values has no effect using boost C++
我正在尝试更改 json 文件中的某些值,但它在 json 文件中不起作用,即使它打印了我在下面所做的更改。
json 文件:
{
"schemaVersion":1,
"array":[
{ //values...
},
{ //the relevant values..
"id":"stackoverflow",
"visible":true,
}
]
}
json文件有效,我刚刚写了相关的东西。
提升代码:
boost::property_tree::ptree doc;
string test = dir_path.string();
boost::property_tree::read_json(test, doc);
BOOST_FOREACH(boost::property_tree::ptree::value_type& framePair2, doc.get_child("array")){
if (!framePair2.second.get<std::string>("id").compare("stackoverflow")){
cout << framePair2.second.get<std::string>("id") << endl;
cout << framePair2.second.get<std::string>("visible") << endl;
framePair2.second.put<string>("visible", "false");
cout << framePair2.second.get<std::string>("visible") << endl;
}
输出:
stackoverflow //which is fine
true //which is also fine
false //which is exactly what I changed and need
问题:
即使 json 文件通过 framePair2.second.put<string>("visible", "false");
打印成功更改,json 文件中也没有更改,我不明白为什么。
为什么在我使用 put 方法后它打印false
并且在 json 文件中它仍然true
?我需要保存 json 文件吗?如果是这样,使用它的命令是什么 提升 ?
任何帮助将不胜感激。
谢谢。
是的,您需要保存 JSON 文件。
对此没有"命令"。而是使用一个函数,就像你使用一个(read_json
)来读取它一样:
- http://www.boost.org/doc/libs/1_57_0/doc/html/boost/property_tree/json_parser/write_json_idp202532560.html
更新
下面是一个示例(从字符串读取,写入 std::cout)。我修复了处理没有 "id"
属性的数组元素的错误。
住在科里鲁
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <sstream>
using namespace boost::property_tree;
std::string const sample = R"(
{
"schemaVersion": 1,
"array": [{
},
{
"id": "stackoverflow",
"visible": true
}]
}
)";
int main() {
ptree doc;
std::istringstream iss(sample);
read_json(iss, doc);
BOOST_FOREACH(ptree::value_type & framePair2, doc.get_child("array")) {
auto id = framePair2.second.get_optional<std::string>("id");
if (id && !id->compare("stackoverflow")) {
std::cout << framePair2.second.get<std::string>("id") << std::endl;
std::cout << framePair2.second.get<std::string>("visible") << std::endl;
framePair2.second.put<std::string>("visible", "false");
std::cout << framePair2.second.get<std::string>("visible") << std::endl;
}
}
write_json(std::cout, doc);
}
输出:
stackoverflow
true
false
{
"schemaVersion": "1",
"array": [
"",
{
"id": "stackoverflow",
"visible": "false"
}
]
}
解决方案:
- Boost 的 json 解析器只在 ptree 中使用字符串,这意味着 ptree 没有对 bool/int. ONLY 字符串等类型的引用。
- 虽然我使用了不太优雅的解决方案,例如使用 ifstream 和 ofstream 类的普通文件操作,但在这里你可以找到(向下滚动到 C/C++ 部分)所有支持类型的 json API。
相关文章:
- 为什么返回类型的'const'限定符对标有 __forceinline/内联的函数没有影响?
- 是否有任何区域设置会影响宽字符编码?
- 此分配对速度有影响吗?
- "virtual"对C++析构函数有何影响?
- 如果我对"while"块发表评论,为什么程序会死机?其中的"yield"线有何影响?
- 当在循环中使用时,std::shared_ptr 对该循环的矢量化有任何影响吗?
- 明确定义'static const variable in a struct'对C++ 11 及以上有什么影响吗?
- 每个帧的参考中都有多少性能影响
- "insert"对德克的引用有什么影响?
- 如果订购有什么影响..否则如果按概率陈述
- 预编译的标题有多少影响MFC
- 等号在大括号初始化中是否有影响?例如。 'T a = {}'与'T a{}'
- 更改静态库的"动态 RTL"设置有什么影响?
- 公共和私有对对象的内存布局是否有任何影响
- 完成的线程是否对 winapi 中的主程序有影响
- 在 c++ 中使用 "this" 关键字对性能有影响吗?
- 额外的继承对对象结构或实例化有影响吗?
- 当do额外的括号有影响时,除了运算符优先级
- Array2的变化对Array(表)有影响
- try-catch对char**返回值有影响