EOS合约内解析json
示例代码
//demo.hpp
#pragma once
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
#include "json.hpp"
#include <string>
using json = nlohmann::json;
using eosio::contract;
using eosio::multi_index;
using eosio::print;
class[[eosio::contract]] demo : public contract
{
public:
using contract::contract;
[[eosio::action]] void demoaction();
};
//demo.cpp
#include "demo.hpp"
using json = nlohmann::json;
// ACTION
void demo::demoaction()
{
json demoInfo = R"(
{
"demoInfo": {
"hello": "world"
}
}
)"_json;
std::string demoInfoString = demoInfo.dump();
json demoInfoJson = json::parse(demoInfoString);
print("It worked\n");
}
EOSIO_DISPATCH(demo, (demoaction))
json.hpp从https://github.com/nlohmann/json/releases 下载并将其放在同一目录中demo.hpp。