Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I tried several solutions but none worked. The problem is that I want to retrieve the values of nonce date pass, for the moment with this program I can only read them. Ideally I should be able to store them in a variable. Please let me know if you have any ideas on how to do this. Thanks

using namespace std;
using json = nlohmann::json;
namespace jsonns {
    struct sha1info {
    string     nonce;
    string     date;
    string     pass;
    void from_json(const json&j, sha1info &p) {
        j.at("nonce").get_to(p.nonce);
        j.at("date").get_to(p.date);
        j.at("pass").get_to(p.pass);
    struct testinfo {
    string      name;
    sha1info   pieces[50];
    int         size;   // piceces size
    void from_json(const json&j, testinfo &t) {
        j.at("name").get_to(t.name);
        for(int i = 0; i < j["tests"].size(); i++) {
            t.pieces[i] = j["tests"][i];
        t.size = j["tests"].size();
int main ()
    json j;
    std::ifstream ifs("/home/nomadea/Bureau/qthash/test.json");
    ifs >> j;
    string tilength = j["nomadea"][0].dump(4);
    cout << tilength;
          "nonce": "6XBXYDrrGZuvqJ/jOIEJ5z72A8E=",
          "date": "2021-03-21T16:14:33Z",
          "pass": "pthamie0102"
          "nonce": "6XBXYDrrGZuvqJ/jOIEJ5z72A8E=",
          "date": "2021-03-21T16:14:33Z",
          "pass": "pthamie0102"
          "nonce": "6XBXYDrrGZuvqJ/jOIEJ5z72A8E=",
          "date": "2021-03-21T16:14:33Z",
          "pass": "pthamie0102"
      "name": "testMd5",
      "tests": [
          "method": "method",
          "realm": "test",
          "password": "oui"

One possibility is to add a constructor from Json to your class like this:

class sha1info {
  public:
    sha1info(std::string const& nonce, std::string const& date, std::string const& pass) noexcept
      : nonce{nonce}, date{date}, pass{pass} {
      return;
    // Method for parsing class from Json
    sha1info(nlohmann::json const& j) {
      nonce = j["nonce"].get<std::string>();
      date = j["date"].get<std::string>();
      pass = j["pass"].get<std::string>();
      return;
    // Method for saving class to Json
    nlohmann::json toJson() const noexcept {
      nlohmann::json j;
      j["nonce"] = nonce;
      j["date"] = date;
      j["pass"] = pass;
      return j;
    // Operator for outputting to console
    friend std::ostream& operator << (std::ostream& os, sha1info const& s) noexcept {
      os << "Piece: nonce: " << s.nonce << ", date: " << s.date << ", pass: " << s.pass;
      return os;
    std::string nonce;
    std::string date;
    std::string pass;
class testinfo {
public:
    testinfo(std::string const& name, std::vector<sha1info> const& pieces) noexcept
      : name{name}, pieces{pieces} {
      return;
    // Method for parsing class from Json
    testinfo(nlohmann::json const& j) {
      name = j["name"].get<std::string>();
      nlohmann::json const& ja = j["tests"];
      for (auto it = ja.begin(); it != ja.end(); ++it) {
        // Call container element constructor from Json
        pieces.emplace_back(*it);
      return;
    // Method for saving class to Json
    nlohmann::json toJson() const noexcept {
      nlohmann::json j;
      j["name"] = name;
      nlohmann::json ja = nlohmann::json::array();
      for (auto const& p: pieces) {
        // Call function of underlying vector element
        ja.push_back(p.toJson());
      j["tests"] = ja;
      return j;
    // Method for returning the size of the encapsulated vector of elements
    std::size_t size() const noexcept {
      return pieces.size();
    // Operator for outputting to console
    friend std::ostream& operator << (std::ostream& os, testinfo const& t) noexcept {
      os << "Testinfo " << t.name << std::endl;
      for (auto const& p : t.pieces) {
        os << p << std::endl;
      return os;
    std::string name;
    std::vector<sha1info> pieces;
int main() {
  // Load file
  std::ifstream ifs;
  ifs.open("test.json");
  // Convert to json
  nlohmann::json j {};
  ifs >> j;
  // Construct testinfo object
  testinfo t {j["nomadea"][0]};
  // Continue processing the file
  // ...
  // Output to console
  std::cout << t << std::endl;
  return EXIT_SUCCESS;
                no need to specify type as assignment operator helps to properly fetch data type: nonce = j["nonce"].get<std::string>(); => nonce = j["nonce"];
– Jean-François Fabre
                May 28, 2021 at 20:56
                @Jean-FrançoisFabre Thanks for you comment. I am aware of that but prefer it as it is more clear what the data type of nonce actually is. For the same reason i prefer writing double const some_double {static_cast<double>(some_int)}; over writing double const some_double = some_int. Might seem a bit verbose to some...
– 2b-t
                May 28, 2021 at 21:00
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.