Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Json.Net反序列化";不适用;对于数值_C#_Json_Json.net_Deserialization_Numeric - Fatal编程技术网

C# Json.Net反序列化";不适用;对于数值

C# Json.Net反序列化";不适用;对于数值,c#,json,json.net,deserialization,numeric,C#,Json,Json.net,Deserialization,Numeric,我正在尝试使用Json.Net反序列化数字数据,如果缺少值,可以将其指定为“NA”,例如 { "readings": [ { "temperature" : 75 } , { "temperature" : "NA" } ] } 我想把这些值解析成一个double?类型,例如 public class Reading { public double? Temperature { get; set; } } 其中(净效应)我的示例中的第一

我正在尝试使用Json.Net反序列化数字数据,如果缺少值,可以将其指定为“NA”,例如

{
    "readings": [
        { "temperature" : 75 } ,
        { "temperature" : "NA" }
    ]
}
我想把这些值解析成一个double?类型,例如

public class Reading
{
    public double? Temperature { get; set; }
}
其中(净效应)我的示例中的第一个读数为“75”,第二个读数为“null”

我对提供给我的JSON没有任何控制权


JsonConverter是让Json.Net为我处理这个问题的唯一方法,还是有更简单的方法?(我总是可以为Json反序列化的值创建一个私有类变量,然后添加我自己的公共属性,该属性可以对私有值执行任何操作,但由于我有很多属性要处理,这将需要很多额外的代码。)

在服务器代码中,将温度设置为模型中的字符串,然后使用
TryParse
查看它是否可以转换为双精度

double tempValue;

if (Double.TryParse(yourObject.Temperature, out temp)){
  // successfully parsed to a double
  // do whatever you're going to do with the value
}
else {
  // couldn't be parsed as a double, handle accordingly
}

我认为没有办法让反序列化程序将字符串转换为可为空的双精度。最好将该值反序列化为字符串,然后在使用该字符串时将其转换为数值

所以你可以试试这样的东西:

 public class Reading
{
public string Temperature {get; set;}
private double? _NumTemperature;
public Double? NumTemperature{
get{ return _NumTemperature} 
set{
Double n;
bool isNumeric = double.TryParse(Temperature, out n);
if (isNumeric)
{
_NumTemperature = n;
}
else
{
_NumTemperature = null;
}
}}
}
public class Reading
{
    public double? Temperature { get; set; }
}

public class Root
{
    public List<Reading> Readings { get; set; }
}
Root obj = JsonConvert.DeserializeObject<Root>(
    json, new JsonSerializerSettings
    {
        Error = (sender, args) =>
        {
            Reading reading = args.CurrentObject as Reading;

            if (reading != null && args.ErrorContext.Member.ToString() == "temperature")
            {
                reading.Temperature = null;
                args.ErrorContext.Handled = true;
            }
        }
    });

我建议构建一个自定义类型转换器,但如果您真的不想,可以忽略反序列化异常。假设您有这样的设置:

 public class Reading
{
public string Temperature {get; set;}
private double? _NumTemperature;
public Double? NumTemperature{
get{ return _NumTemperature} 
set{
Double n;
bool isNumeric = double.TryParse(Temperature, out n);
if (isNumeric)
{
_NumTemperature = n;
}
else
{
_NumTemperature = null;
}
}}
}
public class Reading
{
    public double? Temperature { get; set; }
}

public class Root
{
    public List<Reading> Readings { get; set; }
}
Root obj = JsonConvert.DeserializeObject<Root>(
    json, new JsonSerializerSettings
    {
        Error = (sender, args) =>
        {
            Reading reading = args.CurrentObject as Reading;

            if (reading != null && args.ErrorContext.Member.ToString() == "temperature")
            {
                reading.Temperature = null;
                args.ErrorContext.Handled = true;
            }
        }
    });
Json.NET允许您处理特殊情况;重写一些方法并将其传递给
JsonConvert.Deserialise*
方法

因此,您的步骤是创建一个大致如下的类

public NAConverter: JsonConverter
{
    public override bool CanConvert(Type t) { return t == typeof(string); }
    public override ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        ...
    }
}
然后,在ReadJson中,您可以询问JsonReader是否指向字符串,以及是否为“NA”。如果是,则返回null或zero或Double.IsNaN;否则,请遵循基本原则


然后将一个实例传递给
JsonConvert.DeserializeObject()
JsonConvert.Deserialize()

,您可以按如下方式修改类并用于序列化/反序列化:

public class Reading
{
    [JsonIgnore]
    public double? Temperature { get; set; }

    [JsonProperty("temperature")]
    private string TemperatureString 
    {
        get
        {
            return Temperature.HasValue ? Temperature.Value.ToString() :"NA";
        }
        set
        {
            double result;
            Temperature = double.TryParse(value, out result) ? result : null;
        }
    }
}

您可以使用NewtonSoft的Json库。我在Json.Net文档中发现了这一点。似乎可以基于每个属性指定转换器:

//新日期(976918263055)[JsonProperty][JsonConverter(typeof(JavaScriptDateTimeConverter))]public DateTime LastModified{get;set;}(对格式错误表示歉意——没有仔细阅读帮助。)@BoCoKeith:Yep!这当然也是一个选择。你喜欢什么都行。