Nservicekit反序列化

Nservicekit deserialization

本文关键字:反序列化 Nservicekit      更新时间:2023-09-26

我正试图将一个对象发布到nserviekit Web服务,请求有我的对象,但属性不受影响。但是当我使用JsonConvert进行反序列化时。DeserializeObject我的列表对象已正确填充。有人能帮我做这个吗?我做错了什么?非常感谢。

我正在使用以下代码来实现

[Route("/LogbookRegistries", Summary = @"Posts an List type of LogbookRegistry")]
public class LogbookRegistries
{
    public List<LogBookRegistry> list { get; set; }
}
public class LogbookRegistriesService : Service
{
    public object Options(LogbookRegistries request)
    {
        return true;
    }
    public LogbookRegistriesResponse Post(LogbookRegistries request)
    {
        var list = JsonConvert.DeserializeObject<List<LogBookRegistry>>(Request.FormData["list"]) ;
        Boolean thereAreErrors = false;
      //  if (request.LogbookRegistriesList == null) throw new ArgumentNullException("Cant continue passed data is null");
        try
        {
           // DataBase.SaveList<LogBookRegistry>(request.LogbookRegistriesList);
        }
        catch (Exception ex)
        {
            thereAreErrors = true;
        }
        return new LogbookRegistriesResponse { ResponseStatus = new ResponseStatus { Message = thereAreErrors ? @"There were errors while saving data." : @"Posted data saved succesfully." } };
    }
    public LogbookRegistriesResponse Get(LogbookRegistries request)
    {
        var list = DataBase.GetAll<LogBookRegistry>();
        return new LogbookRegistriesResponse { Data=list };
    }
}
public class LogbookRegistriesResponse
{
    public ResponseStatus ResponseStatus { get; set; }
    public List<LogBookRegistry> Data {get;set;}
}
public partial class LogBookRegistry
{
    public LogBookRegistry()
    {
        this.LogBookRegistryDetails = new HashSet<LogBookRegistryDetail>();
    }

    public string code { get; set; }
    public System.DateTime date { get; set; }
    public int locationId { get; set; }
    public int actionTypeId { get; set; }
    public Nullable<int> deviceId { get; set; }
    public string remarks { get; set; }
    public virtual Location Location { get; set; }
    public virtual Item Item { get; set; }
    public virtual ICollection<LogBookRegistryDetail> LogBookRegistryDetails { get; set; }
    public virtual Device Device { get; set; }
}

在javascript方面,我正在使用

$.ajax({
            type: "POST",
            url: 'http://localhost:49321/LogbookRegistries?',
            data:  {"list":JSON.stringify( [
                        {
                            "LogRegistryDetails": "",
                            "id": "0",
                            "locationId": "2",
                            "actionTypeId": "2",
                            "code": "TRP-OUT-Palmetal-20150310_151929",
                            "active": "true",
                            "date": "2015-03-10 15:19:29",
                            "deviceId": "1",
                            "remarks": ""
                        }
             ])} ,
           dataType: 'json',
        })
            .done(function (data, textStatus, jqXHR) {
                debugger;
                alert('done');
            })
        .fail(function (jqXHR, textStatus, errorThrown) {
            debugger;
            alert('fail');
        });

您需要JSON.stringify()整个JSON数据对象,而不仅仅是列表项数组。然后ServiceStack可以自动取消对请求的序列化。

$.ajax({
        type: "POST",
        url: 'http://localhost:49321/LogbookRegistries?',
        data:  JSON.stringify( {"list":[
                    {
                        "LogRegistryDetails": "",
                        "id": "0",
                        "locationId": "2",
                        "actionTypeId": "2",
                        "code": "TRP-OUT-Palmetal-20150310_151929",
                        "active": "true",
                        "date": "2015-03-10 15:19:29",
                        "deviceId": "1",
                        "remarks": ""
                    }
         ]}) ,
       dataType: 'json',
    })
    .done(function (data, textStatus, jqXHR) {
            debugger;
            alert('done');
        })
    .fail(function (jqXHR, textStatus, errorThrown) {
        debugger;
        alert('fail');
    });