JavaScriptSerializer :: Deserialize()删除超链接

时间:2017-06-23 14:17:56

标签: c# json serialization automapper javascriptserializer

我有一个类似于此的

的AutoMapper映射
AutoMapper.Mapper.CreateMap<Dictionary<string, string>, DailyCheck>().ConvertUsing(
            x =>
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                return serializer.Deserialize<DailyCheck>(
                    Regex.Replace(serializer.Serialize(x),
                    "\\b(?<month>\\d{1,2})/(?<day>\\d{1,2})/(?<year>\\d{2,4})\\b",
                    "${day}-${month}-${year}", RegexOptions.None,
                    TimeSpan.FromMilliseconds(150))
                    );
            }
            );

代码的作用是使用Dictionary并使用JavaScriptSerializer创建一个Json对象,然后我将该对象传递给Deserialize()函数,将其映射到DailyCheck类型的强类型对象。

我遇到的问题是当Deserealizing http链接被删除并且属性被设置为null时。

除了明显的问题:“我如何解决这个问题”我还想问为什么box标准函数JavaScriptSerializer.Serialize()没有正确地转义链接,因此它可以正确反序列化()。

以下是DailyCheck类的内容

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.BusinessLayer
{
    /// <summary>
    /// Business Layer Interface of Daily Checks
    /// </summary>
    public class DailyCheck
    {
        /// <summary>
        /// Identifier of check
        /// </summary>
        public string Id { get; set; }
        /// <summary>
        /// The name of the check
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// Type of check, e.g. Stored procedure
        /// </summary>
        public string Type { get; set; }
        /// <summary>
        /// How often the check should be run
        /// </summary>
        public string Frequency { get; set; }
        /// <summary>
        /// What category does this check relate to?
        /// </summary>
        public string Category { get; set; }
        /// <summary>
        /// The working instruction associated with the check
        /// </summary>
        public string WorkingInstruction { get; set; }
        /// <summary>
        /// Check description
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// Current status of check, E.g. Running/Failed/Succeeded
        /// </summary>
        public string Status { get; set; }
        /// <summary>
        /// The start date of the check, when it was made
        /// </summary>
        public DateTime StartDate { get; set; }
        /// <summary>
        /// The ending date for the check, only not nulled if it is completed
        /// </summary>
        public DateTime? EndDate { get; set; }
        /// <summary>
        /// Transi of last user to edit the check
        /// </summary>
        public string Transi { get; set; }
        /// <summary>
        /// Full name of the last user to edit check 
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// Last comment left for check
        /// </summary>
        public string Comment { get; set; }
        /// <summary>
        /// Iid of user to last sign off check
        /// </summary>
        public string SignedOffBy { get; set; }
        /// <summary>
        /// Id of latest run
        /// </summary>
        public string RunId { get; set; }
        /// <summary>
        /// Id of latest result
        /// </summary>
        public string ResultId { get; set; }
        /// <summary>
        /// Command associated with check
        /// </summary>
        public string Command { get; set; }
    }
}

编辑:您可以使用以下Json对象进行测试

{"id":"1","name":"TEST","type":"SP","frequency":"TD","category":"INT","working_instruction":"http://www.google.co.uk","description":"","command":"UPDATE","status":"NC","startdate":"","enddate":"","transi":"0000","runid":"","comment":"","signedoffby":"","resultid":"","username":""}

观察窗口中显示的原始值如下所示:

"{\"id\":\"335\",\"name\":\"TEST\",\"type\":\"SP\",\"frequency\":\"TD\",\"category\":\"INT\",\"working_instruction\":\"http://www.google.co.uk\",\"description\":\"\",\"command\":\"UPDATE\",\"status\":\"NC\",\"startdate\":\"\",\"enddate\":\"\",\"transi\":\"051w\",\"runid\":\"\",\"comment\":\"\",\"signedoffby\":\"\",\"resultid\":\"\",\"username\":\"\"}"

1 个答案:

答案 0 :(得分:0)

JavaScriptSerializer尽力将属性名称与JSON字段匹配,但由于下划线,不会从名为WorkingInstruction的字段填充名为working_instruction的属性。

将属性重命名为Working_Instruction,或使用[DataContract][DataMember(Name = "working_instruction)]"属性对整个班级进行注释。