NewtonSoft的Json DeserializeObject emtpy GUID字段 [英] NewtonSoft Json DeserializeObject emtpy Guid field

查看:1085
本文介绍了NewtonSoft的Json DeserializeObject emtpy 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
    }
}

不过,我得到一个错误,指出不能转换,为类型GUID

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

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

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.

推荐答案

自定义转换是这样的,但我觉得这是什么东西那么微不足道有点大材小用。

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);
    }
}



用法:

Usage:

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



或者

Alternatively:

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 emtpy GUID字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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