首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >vectorXd from_json()?

vectorXd from_json()?
EN

Stack Overflow用户
提问于 2020-09-04 00:48:35
回答 1查看 193关注 0票数 1

我正在尝试使用eigen::VectorXd和nlohmann-json库实现类的json序列化。将类存储为JSON字符串不是问题。如何从JSON中解析VectorXd?有没有其他的库更适合这个任务?

代码语言:javascript
复制
#include "json.hpp"

class TransformationStep {
public:
  VectorXd support_vector;
  int number;

  TransformationStep(int number_param, VectorXd support_vectorParam) {
    number = number_param;
    support_vector = support_vectorParam;
  }

  ~TransformationStep() {
  }

  //json serialization
  void to_json(nlohmann::json &j);
  void from_json(const nlohmann::json &j);
};


void TransformationStep::to_json(nlohmann::json &j) {
  j["number"] = number;
  j["support_vector"] = support_vector;
}


void Ftf::from_json(const nlohmann::json &j)
{
    number = (j.at("number").get<int>());
    //support_vector = j["support_vector"].get<VectorXd>()); //???
}

-输出调用to_json(nlohmann::json &j)

代码语言:javascript
复制
{
  "number": 3,
  "support_vector": [
    -0.00036705693279489064,
    0.020505439899631835,
    0.3531380358938106,
    0.0017673029092790872,
    -0.9333248513057808,
    0.04670404618976708,
    -0.21905858722244081,
    -1.011945322347849,
    -0.09172040021815037,
    0.008526811888809391,
    0.05187648010664058
  ]
}
EN

回答 1

Stack Overflow用户

发布于 2020-09-04 02:56:29

我想出了

代码语言:javascript
复制
void vector_from_json(VectorXd& vector, const nlohmann::json &j) {
  vector.resize(j.size());
  size_t element_index=0;
  for (const auto& element : j) {
    vector(element_index++) = (double) element;
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63728235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档