我需要以下列格式在 Json 中存储两个结构,即一和二,然后解析此 JSON 并将数据存储回那些结构对象。
{ "One",{
{ "Value1", one.Value1() },
}
},
{ "Two",{
{ "Value1", two.Value1() },
{ "Value2", two.Value2() }
}
},
我能够将数据存储在 json 文件中,但是当我读回它以获取结构值时,出现以下错误:
1>c:\users\212506\documents\visual studio 2015\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(72): error C3867: 'One::Value1': non-standard syntax; use '&' to create a pointer to member
1>c:\users\212506\documents\visual studio 2015\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(72): error C2672: 'nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<_Ty>>>::get_to': no matching overloaded function found
1> with
1> [
1> _Ty=uint8_t
1> ]
1>c:\users\212506\documents\visual studio 2015\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(87): error C2672: 'nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<_Ty>>>::get': no matching overloaded function found
1> with
1> [
1> _Ty=uint8_t
1> ]
1>c:\users\212506\documents\visual studio 2015\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(87): error C2783: 'unknown-type nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<_Ty>>>::get(void) noexcept': could not deduce template argument for '__formal'
1> with
1> [
1> _Ty=uint8_t
1> ]
1> c:\users\212506\documents\visual studio 2015\projects\consoleapplication6\consoleapplication6\nlohmann\json.hpp(20950): note: see declaration of 'nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<_Ty>>>::get'
1> with
1> [
1> _Ty=uint8_t
1> ]
1>c:\users\212506\documents\visual studio 2015\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(87): error C2893: Failed to specialize function template 'unknown-type nlohmann::json_abi_v3_11_2::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::json_abi_v3_11_2::adl_serializer,std::vector<uint8_t,std::allocator<_Ty>>>::get(void) noexcept(<expr>) const'
1> with
1> [
1> _Ty=uint8_t
1> ]
1> c:\users\212506\documents\visual studio 2015\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(87): note: With the following template arguments:
1> c:\users\212506\documents\visual studio 2015\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(87): note: 'ValueTypeCV=One'
1> c:\users\212506\documents\visual studio 2015\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(87): note: 'ValueType=One'
1>c:\users\212506\documents\visual studio 2015\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(87): error C2512: 'One': no appropriate default constructor available
1> c:\users\212506\documents\visual studio 2015\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(35): note: see declaration of 'One'
我正在工作的代码是:
// ConsoleApplication5.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <array>
#include <initializer_list>
#include "nlohmann/json.hpp"
using nlohmann::json;
class Two
{
public:
Two(int val1, int val2) :
m_Value1(val1), m_Value2(val2)
{
}
int Value1()
{
return m_Value1;
}
int Value2()
{
return m_Value2;
}
public:
int m_Value1;
int m_Value2;
};
class One
{
public:
One(int val1) :
m_Value1_1(val1)
{
}
int Value1()
{
return m_Value1_1;
}
public:
int m_Value1_1;
};
std::string ToJsonString(One &one, Two &two)
{
nlohmann::json j =
{
{ "One",{
{ "Value1", one.Value1() },
}
},
{ "Two",{
{ "Value1", two.Value1() },
{ "Value2", two.Value2() }
}
},
};
std::cout << j.dump(4) << std::endl;
std::string str = j.dump(4);
return str;
}
void from_json(const json& j, One& one)
{
j.at("Value1").get_to(one.Value1);
}
void ToObjectsOne(std::string json)
{
nlohmann::json data;
try
{
data = nlohmann::json::parse(json);
}
catch (nlohmann::json::exception& exception)
{
std::cerr << "Exception[" << exception.what() << "]";
}
One one = data["One"].get<One>();
}
int main()
{
One one(10);
Two two(10, 20);
std::string jsonStr = ToJsonString(one, two);
ToObjectsOne(jsonStr);
return 0;
}
请让我知道我们是否可以使用这种方式:
One one = data["One"].get<One>();
如果不是那么我应该如何从两个结构的 json 中获取值。