c++ - 我在 C++ 中遇到了一些关于 boost json 的问题
问题描述
我看到有很多与我非常相似的问题,但我没有看到任何适合我的问题的解决方案。
我正在尝试使用具有以下结构的 boost 库创建 JSON:
{
"event_date": "2018-06-11T09:35:48.867Z",
"event_log": "2018-06-11 09:35:43,253 - recycler [TRACE]: Running recycler::WITHDRAW",
"cassettes": [
{
"value" : "0",
"currency": "BRL",
"CDMType" : "WFS_CDM_TYPEREJECTCASSETTE",
"lppPhysical" : [
{
"positionName" : "BIN1A",
"count" : "3"
}
]
},
{.....},{.....}
]
}
下面我们将拥有我现在拥有的代码:
boost::property_tree::ptree children, children2, child, child1, child2, child3, child4, child5, cassettes;
child1.put("value", "cash_unit->ulValues");
child2.put("currency", "std::string(cash_unit->cCurrencyID).substr(0, 3)");
child3.put("CDMType", "cash_unit->usType");
child4.put("lppPhysical.positionName", "ph_unit->lpPhysicalPositionName");
child5.put("lppPhysical.count", "cash_unit->ulCount");
cassettes.put("event_date", "2018-06-11T09:35:48.867Z");
cassettes.put("event_log", "2018-06-11 09:35:43,253 - recycler [TRACE]: Running recycler::WITHDRAW");
children.push_back(std::make_pair("", child1));
children.push_back(std::make_pair("", child2));
children.push_back(std::make_pair("", child3));
children2.push_back(std::make_pair("", child4));
children2.push_back(std::make_pair("", child5));
cassettes.add_child("cassettes", children);
write_json("C:\\Temp\\test.json", cassettes);`
总而言之,我很难将对象数组放入对象数组中。
解决方案
终于找到了我的案例的解决方案,它非常简单但很难找到,因为我对这个库不太熟悉。
//LppPhysical insertion
lppPhysicalInfo.put("positionName", ph_unit->lpPhysicalPositionName);
lppPhysicalInfo.put("count", cash_unit->ulCount);
lppPhysical.push_back(std::make_pair("", lppPhysicalInfo));
//Cassettes insertions
cassettesInfo.put("value", cash_unit->ulValues);
cassettesInfo.put("currency", std::string(cash_unit->cCurrencyID).substr(0, 3));
cassettesInfo.put("CDMType", cash_unit->usType);
cassettesInfo.add_child("lppPhysical", lppPhysical.get_child(""));
cassettes.push_back(std::make_pair("", cassettesInfo));
//External information insertion
arquivo.put("event_date", "DateValue");
arquivo.put("event_log", "LogValue");
arquivo.add_child("cassettes", cassettes);
//Json creator
write_json("C:\\Temp\\assassino.json", arquivo);
在 lppPhysical 上,我只是用它的所有内容制作了一对,在 Cassettes 的插入上,我只是添加了 lppPhysical 作为磁带的子项,仅此而已。现在 lppPhysical 是 Cassettes 中的一个对象数组,它也是一个对象数组
推荐阅读
- c# - 获取 .net core 中的开放通用类型列表
- java - 如何在slider zoom sdk android中隐藏参与者?
- javascript - console.log 正在工作,但我无法收到任何警报。三个警报都没有出现。需要做什么?
- json - 从 json discord.py 中随机选择
- python - 如何使用 SIFT/SURF/KAZE 等特征提取器针对一张目标图像创建相似度分数
- python - 自动化要求输入密码的 bash 脚本
- c# - Azure 函数队列触发器添加 Nuget 包
- logging - 根据结果字段/字符串值获取 Splunk Query 的不同结果(过滤结果)
- swiftui - 在 SwiftUI 的模态 .sheet() 中显示共享表(UIActivityViewController)
- python - 为什么 str.replace("\\","/") 不起作用?