C# 使用Json.net反序列化数据类型
我想反序列化以下jsonC# 使用Json.net反序列化数据类型,c#,json.net,C#,Json.net,我想反序列化以下json { “variableName”:“当前”, “数据格式”:“浮动” } 并希望直接将“dataformat”作为变量的数据类型获取 在这种情况下 public string VariableName {get; set;} public float VariableValue {get; set;} // or public boolean VariableValue {get;set;} // or public object VariableValue {ge
{
“variableName”:“当前”,
“数据格式”:“浮动”
}
并希望直接将“dataformat”作为变量的数据类型获取
在这种情况下
public string VariableName {get; set;}
public float VariableValue {get; set;}
// or
public boolean VariableValue {get;set;}
// or
public object VariableValue {get; set;}
是否有任何建议?您可以在getter for VariableValue中封装一个类型转换器。例如
void Main()
{
const string json = @" {
'variableName': 'Current',
'dataFormat': 'System.Double',
'dataValue' : '1.2e3' //scientific notation
}";
var v = JsonConvert.DeserializeObject<Variable>(json);
Console.WriteLine($"Value={v.VariableValue}, Type={v.VariableValue.GetType().Name}");
// Value=1200, Type=Double
// Note that it converted the string "1.2e3" to a proper numerical value of 1200.
// And recognises that VariableValue is a Double instead of our declared Object.
}
public class Variable
{
// From JSON:
public string VariableName { get; set; }
public string DataFormat { get; set; }
public string DataValue { get; set; }
// Here be magic:
public object VariableValue
{
get
{
/* This assumes that 'DataFormat' is a valid .NET type like System.Double.
Otherwise, you'll need to translate them first.
E.g. "FLOAT" => "System.Single"
"INT" => "System.Int32", etc
*/
var actualType = Type.GetType(DataFormat, true, true);
return Convert.ChangeType(DataValue, actualType);
}
}
}
void Main()
{
常量字符串json=@{
'variableName':'Current',
'dataFormat':'System.Double',
'dataValue':'1.2e3'//科学记数法
}";
var v=JsonConvert.DeserializeObject(json);
WriteLine($“Value={v.VariableValue},Type={v.VariableValue.GetType().Name}”);
//值=1200,类型=Double
//请注意,它将字符串“1.2e3”转换为适当的数值1200。
//并认识到VariableValue是一个Double,而不是我们声明的对象。
}
公共类变量
{
//从JSON:
公共字符串变量名{get;set;}
公共字符串数据格式{get;set;}
公共字符串数据值{get;set;}
//这里是魔术:
公共对象变量值
{
得到
{
/*这假设“DataFormat”是有效的.NET类型,如System.Double。
否则,您需要先翻译它们。
例如,“浮动”=>“系统单”
“INT”=>“System.Int32”等
*/
var actualType=Type.GetType(DataFormat,true,true);
返回Convert.ChangeType(数据值、实际类型);
}
}
}
编辑:对于从类型别名到框架类型的转换(例如,
float
到System.Single
),.您可以在getter中为VariableValue封装一个类型转换器。例如
void Main()
{
const string json = @" {
'variableName': 'Current',
'dataFormat': 'System.Double',
'dataValue' : '1.2e3' //scientific notation
}";
var v = JsonConvert.DeserializeObject<Variable>(json);
Console.WriteLine($"Value={v.VariableValue}, Type={v.VariableValue.GetType().Name}");
// Value=1200, Type=Double
// Note that it converted the string "1.2e3" to a proper numerical value of 1200.
// And recognises that VariableValue is a Double instead of our declared Object.
}
public class Variable
{
// From JSON:
public string VariableName { get; set; }
public string DataFormat { get; set; }
public string DataValue { get; set; }
// Here be magic:
public object VariableValue
{
get
{
/* This assumes that 'DataFormat' is a valid .NET type like System.Double.
Otherwise, you'll need to translate them first.
E.g. "FLOAT" => "System.Single"
"INT" => "System.Int32", etc
*/
var actualType = Type.GetType(DataFormat, true, true);
return Convert.ChangeType(DataValue, actualType);
}
}
}
void Main()
{
常量字符串json=@{
'variableName':'Current',
'dataFormat':'System.Double',
'dataValue':'1.2e3'//科学记数法
}";
var v=JsonConvert.DeserializeObject(json);
WriteLine($“Value={v.VariableValue},Type={v.VariableValue.GetType().Name}”);
//值=1200,类型=Double
//请注意,它将字符串“1.2e3”转换为适当的数值1200。
//并认识到VariableValue是一个Double,而不是我们声明的对象。
}
公共类变量
{
//从JSON:
公共字符串变量名{get;set;}
公共字符串数据格式{get;set;}
公共字符串数据值{get;set;}
//这里是魔术:
公共对象变量值
{
得到
{
/*这假设“DataFormat”是有效的.NET类型,如System.Double。
否则,您需要先翻译它们。
例如,“浮动”=>“系统单”
“INT”=>“System.Int32”等
*/
var actualType=Type.GetType(DataFormat,true,true);
返回Convert.ChangeType(数据值、实际类型);
}
}
}
编辑:用于从类型别名到框架类型的转换(例如,
float
到System.Single
),.JSON中的变量值在哪里?是的,这是我的问题。。。对于VariableName,我使用左侧,对于要使用右侧的数据类型,我使用右侧。VariableValue也可以有另一个名称。。。可以理解吗?我不确定我是否理解。在JSON中,数据格式表示数据类型。在C#中,VariableValue
应该表示一些数据,如5.4
。但我看不到JSON中的值在哪里。JSON中的变量值在哪里?是的,这是我的问题。。。对于VariableName,我使用左侧,对于要使用右侧的数据类型,我使用右侧。VariableValue也可以有另一个名称。。。可以理解吗?我不确定我是否理解。在JSON中,数据格式表示数据类型。在C#中,VariableValue
应该表示一些数据,如5.4
。但我不知道JSON中的值在哪里。