1

我有一些像这样的json数据:

{
    'vehicles':{
    ...
    },
    'arenaUniqueID':214197923159382012L,
    'common':{
    ...
    },
    'players':{
    ...
    },
    'personal':{
    ...
    }
}

和用于反序列化此数据的类

public class UnPickledData
{
    public Dictionary<int, Vehicle> vehicles { get; set; }        
    public long arenaUniqueID { get; set; }
    public Common common { get; set; }
    public Dictionary<int, Player> players { get; set; }
    public Personal personal { get; set; }
}

在反序列化程序中抛出异常:

解析值后遇到意外字符:L. Path arenaUniqueID',第 1 行,位置 51810。

我试图为该属性编写自己的自定义 JSonConverter:更改类:

public class UnPickledData
{
    public Dictionary<int, Vehicle> vehicles { get; set; }
    [JsonConverter(typeof(CustomLongConverter))]
    public Int64 arenaUniqueID { get; set; }
    public Common common { get; set; }
    public Dictionary<int, Player> players { get; set; }
    public Personal personal { get; set; }
}

转换器代码:

class CustomLongConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(long);
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        return Int64.Parse(reader.Value.ToString());           
    }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotSupportedException();
    }
}

但这个异常仍然发生。

我做错了什么以及如何解决这个问题?

ps 使用 Newtonsoft.JSON v5.0.5.16108/.Net FW 3.5/VS 2010

4

1 回答 1

3

The L after the number isn't valid JSON.

于 2013-06-17T03:57:17.427 回答