如何将json数据发送到可序列化类

Posted

技术标签:

【中文标题】如何将json数据发送到可序列化类【英文标题】:how to send json data to serializable class 【发布时间】:2014-12-03 04:12:53 【问题描述】:

将json数据发送到可序列化类时出现问题。在客户端,当我在浏览器上检查数据时,这是真的,但在服务器端,可序列化类的字段为空。

客户端(使用dojo):在调试器行上分配票对象的字段

var Ticket = 
        ProductId: productId,
        Type: ticketType
    ;

    DataService.xhrPageMethodJson("api/Tickets/MyTickets"
        , dojo.toJson(Ticket)
        , 
            success: function (data) 
                debugger
                loadSubPage(getRoot("Support/Tickets.aspx?mode=list"));
            ,
            error: function (err) 
                LoadingOverlay.hide();
            
        );

Web Method:在此方法ticket.ProductId上,ticket.Type字段具有默认值或null。

    [HttpPost]
    [AcceptVerbs("POST")]
    [ActionName("MyTickets")]
    [Authorize]
    public void MyTickets([FromBody]Ticket ticket)
    
        DataTable myTickets = db.Tickets.MyTickets(Globals.LogonUser.User.Id, ticket.ProductId, ticket.Type);
        WebContext.Session["Tickets"] = myTickets;
    

票类:

[Serializable]
[EntityTable(Name = "Tickets")]
public class Ticket : Entity

    public override int Id  get; set; 
    public string Subject  get; set; 
    public string Message  get; set; 
    public DateTime SentDate  get; set; 
    [JoinField(typeof(User), "Id")]
    public int SenderUserId  get; set; 
    [JoinField(typeof(Product), "Id")]
    public int ProductId  get; set; 
    [JoinField(typeof(TicketImportanceLevel), "Id")]
    public int ImportanceLevel  get; set; 
    public int Analyze  get; set; 
    public int SolutionTransactionId  get; set; 
    [JoinField(typeof(TicketType), "Id")]
    public int Type  get; set; 
    public int Status  get; set; 
    public int AssignedUserId  get; set; 
    public float Score  get; set; 


【问题讨论】:

【参考方案1】:

我已经解决了这个问题,将属性标记为数据成员,将类标记为数据契约。我在这里分享新的班级结构:

[Serializable]
[DataContract]
[EntityTable(Name = "Tickets")]
public class Ticket : Entity

    [DataMember]
    public override int Id  get; set; 
    [DataMember]
    public string Subject  get; set; 
    [DataMember]
    public string Message  get; set; 
    [DataMember]
    public DateTime SentDate  get; set; 
    [JoinField(typeof(User), "Id")]
    [DataMember]
    public int SenderUserId  get; set; 
    [JoinField(typeof(Product), "Id")]
    [DataMember]
    public int ProductId  get; set; 
    [JoinField(typeof(TicketImportanceLevel), "Id")]
    [DataMember]
    public int ImportanceLevel  get; set; 
    [DataMember]
    public int Analyze  get; set; 
    [DataMember]
    public int SolutionTransactionId  get; set; 
    [JoinField(typeof(TicketType), "Id")]
    [DataMember]
    public int Type  get; set; 
    [DataMember]
    public int Status  get; set; 
    [DataMember]
    public int AssignedUserId  get; set; 
    [DataMember]
    public float Score  get; set; 

【讨论】:

以上是关于如何将json数据发送到可序列化类的主要内容,如果未能解决你的问题,请参考以下文章

如何使用json.net自定义反序列化为对象

如何将json序列化为sql?

C# webApi----H5_app后台制作--序列化的类无法接收Json格式的数据

C#如何生成JSON字符串?(序列化对象)

如何在 Django 中序列化(JSON)文件字段

post发送JSON数据(字符串、数组、字典、自定义对象)给服务器