You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The elements under "matrix" are 2-D-arrays of floats, which are of importance for other parts of the application. I have found a solution to write the matrix field to a variable of type std::vector<std::vector<float>>. The code for that is below:
#include <iostream>
#include <cstdint>
#include <string>
#include <fstream>
#include <vector>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main ()
{
std::string file = "test.json";
std::ifstream ifs(file);
json j = json::parse(ifs);
uint64_t id = 10;
// empty initialization
json sensorcfg(json::value_t::object);
// if the ID is found, retrieve the config
for (auto it : j["sensors"]) {
if (it["id"].get<uint64_t>() == id) {
sensorcfg = it;
break;
}
}
std::vector<std::vector<float>> m;
if (!sensorcfg.empty()) {
m = sensorcfg["matrix"].get<std::vector<std::vector<float>>>();
for(auto i : m) {
for(auto j : i) {
std::cout << j << std::endl;
}
}
}
}
The problem with this solution is that I must not use vectors when defining m, but a standard C array like m[3][3]. Is there an elegant way to do the conversion? I tried calling get<float[3][3]>(), but this is not working.
I would be grateful for a reply. Thanks!
The text was updated successfully, but these errors were encountered:
Unfortunately, C arrays are currently not supported out of the box. To avoid creating a vector first, you could need to iterate sensorcfg["matrix"] yourself and copy the values to the destination array.
I have a question regarding the conversion of a json array into a standard C++ array (not a vector!). I have a file that looks basically like this:
The elements under "matrix" are 2-D-arrays of floats, which are of importance for other parts of the application. I have found a solution to write the matrix field to a variable of type
std::vector<std::vector<float>>
. The code for that is below:The problem with this solution is that I must not use vectors when defining
m
, but a standard C array likem[3][3]
. Is there an elegant way to do the conversion? I tried callingget<float[3][3]>()
, but this is not working.I would be grateful for a reply. Thanks!
The text was updated successfully, but these errors were encountered: