c++ - 如何反序列化一个数组?
问题描述
我正在使用nlohmann::json库来序列化/反序列化json
. 下面是我如何序列化一个C++
双精度数组:
double mLengths[gMaxNumPoints] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
...
nlohmann::json jsonEnvelope;
jsonEnvelope["lengths"] = envelope.mLengths;
哪个产品:
"lengths":[
1.0,
2.0,
3.0,
4.0,
5.0
]
但是现在,我怎样才能反序列化回mLengths
?试过:
mLengths = jsonData["envelope"]["lengths"];
但它说expression must be a modifiable lvalue
。如何恢复阵列?
解决方案
它适用于矢量:
#include <iostream>
#include <nlohmann/json.hpp>
int main() {
double mLengths[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
nlohmann::json j;
j["lengths"] = mLengths;
std::cout << j.dump() << "\n";
std::vector<double> dbs = j["lengths"];
for (const auto d: dbs)
std::cout << d << " ";
std::cout << "\n";
return 0;
}
通过赋值进行反序列化不适用于 C 数组,因为您无法为它们正确定义转换运算符。
推荐阅读
- reactjs - 在 reactjs 上从 Axios 服务器渲染数组
- javascript - 如何使用 Eclipse 运行 ElectronJS 应用程序?
- ios - Xamarin WebView - iOS - 在应用程序运行之间存储 cookie
- javascript - reactjs data.length 在保存时可访问,但在浏览器刷新时不可访问
- python - Python ThreadPoolExecutor 监听关闭触发器
- c# - 使用 ASP.NET Core 从任何网站管理多个帐户
- mongodb - 如何正确合并mongoBD的集合?
- visual-studio-code - 如何更改命令调色板中匹配单词的颜色?
- android-studio - 布局已经受到约束,但文本仍未显示
- excel - 如何转置脚本字典键并将“05”和“5”保留为两个不同的值?