如何在C#中使用JavascriptSerializer序列化嵌套实体(模型)?

时间:2016-04-17 09:37:35

标签: c# json asp.net-mvc nested javascriptserializer

我正在构建一个ASP.NET Web应用程序,我也在使用Entity Framework Code First。在这种情况下,我使用两个实体(客户和联系人),它们彼此具有一对多关系(一个客户可以有多个联系人)。从数据库获取数据时,一切都很顺利。我也使用Viewmodels,所以在我的数据实体旁边,我还有两个名为CustomerModel和ContactModel的模型。

下面我将展示我的实体和我的视图模型:

客户实体

[Table("Customer")]
    public class Customer
    {
        [Key()]
        public Guid Id { get; set; }
        //other non-relevant properties
        public virtual List<Contact> Contacts { get; set; }
    }

联系实体

[Table("Contact")]
    public class Contact
    {
        [Key()]
        public Guid Id { get; set; }
        //non-relevant properties
        [ForeignKey("Customer")]
        public Guid CustomerId { get; set; }
        [Required]
        public virtual Customer Customer { get; set; }
    }

CustomerModel

[Serializable]
    public class CustomerModel
    {
        public Guid Id { get; set; }
        //non-relevant properties
        [ScriptIgnore(ApplyToOverrides = true)]
        public virtual List<ContactModel> Contacts { get; set; }
    }

ContactModel

[Serializable]
    public class ContactModel
    {
        public Guid Id { get; set; }
        //non-relevant properties
        public Guid CustomerId { get; set; }
        [ScriptIgnore(ApplyToOverrides = true)]
        public virtual CustomerModel Customer { get; set; }
    }

当我运行我的应用程序和代码时,它在后端工作正常,直到它需要在我的HomeController中将其序列化为JSON。

public ActionResult GetCustomerById(Guid id)
        {
            CustomerModel customer = new CustomerManager().GetById(id);
            string output = serializer.Serialize(customer);
            return Content(output);
        }

即使CustomerModel对象获得了2个联系人,但在我的Serialize(customer)方法中,它并未将其解析为JSON。实际上,当我调试它并查看我的输出时,我会看到每个属性,但不会看到嵌套的ContactModels。

就我而言,string output并不包含两个Contacts。我该如何解决这个问题?我检查了一些类似的&#39;关于Stackoverflow的问题,但没有结果。

1 个答案:

答案 0 :(得分:1)

好的,我发现你缺少一个概念DTO和循环引用,简单来说,你必须为你想要提供的内容建立一个模型。什么是咬你的是层/参考,所以我废除了我们模型中的所有循环。

想想 - &gt; b - &gt;这会导致json看起来像这样

{ A:B {A:B {A:B {A:B {A:B {A:B {A:B {A:B {A:B {A:B {A}}}}}}}}} } //进入无限和超越 }

这实际上就是你想要的。

public JsonResult GetCustomerById(Guid id)
{
    CustomerModel customer = new CustomerManager().GetById(id);
    CustomerResultModel output = new CustomerResultModel(){
        id = customer.Id,
        Contacts = GetContacts(customer.Contacts)
    };
    return Json(output, JsonRequestBehavior.AllowGet); 
    //If you only POST then remove the AllowGet (the action name says Get so I'm assuming
}
private IEnumerable<ContactResultModel> GetContacts(Contacts){
    foreach(var a in Contacts){
        //When you use the yield keyword in a statement, you indicate that the method, operator, or get accessor in which it appears is an iterator.
        yield return new ContactResultModel(){            
            Id  = a.Id,
            CustomerId = a.CustomerId
        };
    }
}

模型

class CustomerResultModel{
    public Guid id {get;set;}
    public IEnumerable<ContactResultModel> Contacts {get;set;}
}

class ContactResultModel{
    public Guid id {get;set;}
    public Guid CustomerId {get;set;}
}