簡體   English   中英

如何從 nlohmann json 獲得價值

[英]How to get value from nlohmann json

我嘗試了幾種解決方案,但都沒有奏效。 問題是我想檢索 nonce date pass 的值,目前使用這個程序我只能讀取它們。 理想情況下,我應該能夠將它們存儲在一個變量中。 如果您對如何執行此操作有任何想法,請告訴我。 謝謝

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;
     
     
   


  
    return 0;
 
  
 
}

這是我的 json 文件:

{
  "nomadea": [
    {
      "name": "testSha1",
      "tests": [
        {
          "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"
        }
      ]
    }
  ]
}

一種可能性是將 Json 的構造函數添加到 class 中,如下所示:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM