2014-02-20 42 views
3

我使用ASP.NET MVC C#与HTML CSS jQuery KnockoutJs前端。NewtonSoft Json DeserializeObject空Guid字段

我在我的HTML页面上有模态联系表单。这个想法是,如果我创建一个新的联系人模式窗体弹出空白值包括一个空白的隐藏ID字段。

如果我编辑一个联系人,那么模式表单会弹出填充字段,包括隐藏的id字段。

在我的控制,我打算这样做:

public JsonResult Contact(string values) 
{ 
    var contact = JsonConvert.DeserializeObject<contact>(values); 

    if (contact.Id.Equals(Guid.Empty)) 
    { 
     // create a new contact in the database 
    } else 
    { 
     // update an existing one 
    } 
} 

不过,我得到一个错误,指出can't convert "" to type Guid

你如何解决这个问题有NewtonSoft JSON,我已经在看着here自定义JsonConverter它似乎是沿着正确的路线,但我不知道该去哪里。

+0

您是否尝试过制作'contact.Id'属性为空的?这可能会解决它。 –

+0

我使用数据库中使用的“Contact”模型,因此使它可为空将会很糟糕。 –

回答

6

一个自定义转换器看起来像这样,但我觉得这对一些如此微不足道的东西有点矫枉过正。

/// <summary> 
/// Converts a <see cref="Guid"/> to and from its <see cref="System.String"/> representation. 
/// </summary> 
public class GuidConverter : JsonConverter 
{ 
    /// <summary> 
    /// Determines whether this instance can convert the specified object type. 
    /// </summary> 
    /// <param name="objectType">Type of the object.</param> 
    /// <returns>Returns <c>true</c> if this instance can convert the specified object type; otherwise <c>false</c>.</returns> 
    public override bool CanConvert(Type objectType) 
    { 
     return objectType.IsAssignableFrom(typeof(Guid)); 
    } 

    /// <summary> 
    /// Reads the JSON representation of the object. 
    /// </summary> 
    /// <param name="reader">The <see cref="JsonReader"/> to read from.</param> 
    /// <param name="objectType">Type of the object.</param> 
    /// <param name="existingValue">The existing value of object being read.</param> 
    /// <param name="serializer">The calling serializer.</param> 
    /// <returns>The object value.</returns> 
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     try 
     { 
      return serializer.Deserialize<Guid>(reader); 
     } 
     catch 
     { 
      return Guid.Empty; 
     } 
    } 

    /// <summary> 
    /// Writes the JSON representation of the object. 
    /// </summary> 
    /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param> 
    /// <param name="value">The value.</param> 
    /// <param name="serializer">The calling serializer.</param> 
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     serializer.Serialize(writer, value); 
    } 
} 

用法:

class Contact 
{ 
    [JsonConverter(typeof(GuidConverter))] 
    public Guid Id { get; set; } 
} 

或者:

var contact = JsonConvert.DeserializeObject<contact>(values, new GuidConverter()); 

编辑

我相信,你的JSON看起来像这个有很多:

{ 
    "id": "", 
    "etc": "..." 
} 

的问题很可能是固定的,如果你能做到这一点,而不是:

{ 
    "id": null, 
    "etc": "..." 
} 
+0

现在就进行测试 –

+0

本质上,它所做的就是将默认转换器包装在一个'try-catch'块中。如果转换失败,它默认为'Guid.Empty'。不是最有效的解决方案。例如,你可以使用'String.IsNullOrEmpty()'来避免第一个异常。 –

+1

最好的解决方案是根本不包括JSON数据中的'Id'属性。你能做到吗? –