json.net反序列化具有字典属性以及其他属性的对象

时间:2013-04-23 20:54:35

标签: json.net

我有一个表示以下对象类型的JSON字符串:

public partial class Address
{
    public Address()

    public string Country { get; set; }
    public string StreetNo { get; set; }
    public string City { get; set; }
    public string Zip { get; set; }
    public string Complement { get; set; }
    public Nullable<int> Latitude { get; set; }
    public Nullable<int> Longitude { get; set; }
    public Nullable<int> Altitude { get; set; }
    public string Url { get; set; }
    public System.DateTime LastModified { get; set; }
    public byte[] Version { get; set; }
    public string StateCode { get; set; }
    public string Street { get; set; }
    public string RecordStatus { get; set; }
    public int UserId { get; set; }
    public int AddressId { get; set; }
    public Dictionary<string, object> OriginalValues { get; set; }
}

我正在尝试创建一个JsonConverter来将JSON字符串反序列化为上述对象。我坚持要添加到我的转换器中以在我的代码中创建字典。到目前为止,转换器的代码如下所示:

    public class AddressConverter : JsonConverter
{

    public override bool CanConvert(Type objectType)
    {
        var temp = objectType == typeof(Address);
        return temp;
    }

    public override object ReadJson(JsonReader reader, Type objectType
        , object existingValue, JsonSerializer serializer)
    {
        var obj = JObject.Load(reader);

        var model = new Address();

        model.Country = Convert.ToString(((JValue)obj["Country"]).Value);
        model.StreetNo = Convert.ToString(((JValue)obj["StreetNo"]).Value);
        model.City = Convert.ToString(((JValue)obj["City"]).Value);
        model.Zip = Convert.ToString(((JValue)obj["Zip"]).Value);
        model.Complement = Convert.ToString(((JValue)obj["Complement"]).Value);
        model.Latitude = Convert.ToInt32(((JValue)obj["Latitude"]).Value);
        model.Longitude = Convert.ToInt32(((JValue)obj["Longitude"]).Value);
        model.Altitude = Convert.ToInt32(((JValue)obj["Altitude"]).Value);
        model.Url = Convert.ToString(((JValue)obj["Url"]).Value);
        model.LastModified = Convert.ToDateTime(((JValue)obj["LastModified"]).Value);
        model.StateCode = Convert.ToString((((JValue)obj["Country"]).Value));
        model.Street = Convert.ToString(((JValue)obj["StateCode"]).Value);
        model.RecordStatus = Convert.ToString(((JValue)obj["RecordStatus"]).Value);
        model.UserId = Convert.ToInt32(((JValue)obj["UserId"]).Value);
        model.AddressId = Convert.ToInt32(((JValue)obj["AddressId"]).Value);

        var encodedString = ((JValue) obj["Version"]).Value.ToString();
        model.Version = Convert.FromBase64String(encodedString);

//在这里添加词典代码

        return model;
    }

    public override bool CanWrite
    {
        get
        {
            return false;
        }
    }
    public override void WriteJson(JsonWriter writer, object value
        , JsonSerializer serializer)
    {
        serializer.Serialize(writer, value);
        throw new NotImplementedException();
    }
}

1 个答案:

答案 0 :(得分:0)

我不知道你想用这个自定义转换器实现什么。但这就是你想要的。

// Dictionary code here
model.OriginalValues = (Dictionary<string, object>)JsonConvert.DeserializeObject(obj["OriginalValues"].ToString(), typeof(Dictionary<string, object>));