输入字符串不是有效数字 - JSON反序列化

时间:2016-05-08 17:40:09

标签: c# json.net

我有这个数组:

{
    "AssemblyVersion":"0.1.333.5973",
    "Exception":
    {
        // [...]
    }
}

从这个类序列化:

public class ErrorData
{
    public string AssemblyVersion { get; private set; }
    public Exception Exception { get; private set; }

    public ErrorData(string assemblyVersion, Exception ex)
    {
        AssemblyVersion = assemblyVersion;
        Exception = ex;
    }

    public override string ToString()
    {
        return JsonConvert.SerializeObject(this);
    }
}

将其序列化回对象会产生以下异常:

Newtonsoft.Json.JsonReaderException: Input string '0.1.335.5973' is not a valid number. Path 'AssemblyVersion', line 1, position 29.
    at Newtonsoft.Json.JsonTextReader.ParseNumber(ReadType readType)
    at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)
    at Newtonsoft.Json.JsonTextReader.ReadAsString()
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolvePropertyAndCreatorValues(JsonObjectContract contract, JsonProperty containerProperty, JsonReader reader, Type objectType)
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor`1 creator, String id)
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCreator)
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
    at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
    at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
    at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
    at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)

简而言之,我使用重写的ToString()方法将ErrorData实例序列化为json字符串,将结果作为命令行参数传递给子进程,该进程将其反序列化为ErrorData实例

我错过了什么?似乎AssemblyVersion字符串被视为数字而不是字符串。

编辑:

我以这种方式反序列化:

ErrorData errorData = JsonConvert.DeserializeObject<ErrorData>(args[0]);

序列化已在上面的代码中显示,我将其实现为ToString()方法。

我可以验证它是否发生,因为我将数组作为命令行arg传递。这段代码适用于:

ErrorData errorData = Json.DeserializeObject<ErrorData>(new ErrorData("", new Exception()).ToString());

1 个答案:

答案 0 :(得分:0)

我建议您检查输入,因为下面的代码会抛出有关无效数字的异常:

JsonConvert.DeserializeObject<ErrorData>("{'AssemblyVersion':0.1.333.5973,'Exception':{}}");

但如果引用AssemblyVersion值,则反序列化将成功:

JsonConvert.DeserializeObject<ErrorData>("{'AssemblyVersion':'0.1.333.5973','Exception':{}}");