nlohmann json:检查类型并取出数据
经常需要获取json中的某个key是否存在,如果存在的话取出其中的数据,可以参照下例:
#include <iostream>
#include <nlohmann/json.hpp>
using namespace std;
using json = nlohmann::json;template <typename T>
struct is_std_vector : std::false_type {};template <typename T, typename Alloc>
struct is_std_vector<std::vector<T, Alloc>> : std::true_type {};template <typename T>
bool checkJsonType(const nlohmann::json& j)
{if constexpr (std::is_same_v<T, int>)return j.is_number_integer();else if constexpr (std::is_same_v<T, double>)return j.is_number();else if constexpr (std::is_same_v<T, std::string>)return j.is_string();else if constexpr (std::is_same_v<T,