NewtonSoft Json DeserializeObject空Guid字段 [英] NewtonSoft Json DeserializeObject empty Guid field

查看:159
本文介绍了NewtonSoft Json DeserializeObject空Guid字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将ASP.NET MVC C#与HTML CSS jQuery KnockoutJs前端一起使用.

I'm using ASP.NET MVC C# with HTML CSS jQuery KnockoutJs frontend.

我的HTML页面上有一个模态联系表.这个想法是,如果我创建一个新的联系人,则会弹出模态表格,其中包含空白值,包括一个隐藏的id字段.

I have a modal contact form on my HTML page. The idea is that if I create a new contact the modal form pops up with blank values including a blank hidden id field.

如果我编辑联系人,则会弹出模态表单,其中包含已填写的字段,包括该隐藏的id字段.

If I edit a contact then the modal form pops up with filled out fields including that hidden id field.

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

In my controller I intend to do this:

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

However, I get an error saying that can't convert "" to type Guid

如何使用NewtonSoft Json解决这个问题,我已经自定义JsonConverter 的"nofollow>此处,它似乎是正确的路线,但是我不确定该在哪里使用.

How do you get around this with NewtonSoft Json, i've looked here at the Custom JsonConverter and it seems to be along the right lines, however I'm not sure where to go with this.

推荐答案

自定义转换器看起来像这样,但是我觉得这对于一些琐碎的事情来说有点矫kill过正.

A custom converter would look like this, but I feel like it's a bit overkill for something so trivial.

/// <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看起来很像这样:

I presume that your JSON looks a lot like this:

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

如果您可以改为这样做,则该问题可能会得到解决:

The problem would probably be fixed if you can do this instead:

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

这篇关于NewtonSoft Json DeserializeObject空Guid字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆