Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法通过GET反序列化数组_C#_Jquery_Json_Kendo Ui_<img Src="//i.stack.imgur.com/WM7S8.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">servicestack - Fatal编程技术网 servicestack,C#,Jquery,Json,Kendo Ui,servicestack" /> servicestack,C#,Jquery,Json,Kendo Ui,servicestack" />

C# 无法通过GET反序列化数组

C# 无法通过GET反序列化数组,c#,jquery,json,kendo-ui,servicestack,C#,Jquery,Json,Kendo Ui,servicestack,我正在使用Kendo UI将排序信息发送到我的ServiceStack服务。我知道,如果您使用的是POST,那么这就是问题所在,但我希望坚持RESTful设计,因此请求动词必须是GET。我正在使用此代码段测试: var dataSource = new kendo.data.DataSource({ serverSorting: true, sort: [{ field: "ProductName", dir: "desc" }, {

我正在使用Kendo UI将排序信息发送到我的ServiceStack服务。我知道,如果您使用的是
POST
,那么这就是问题所在,但我希望坚持RESTful设计,因此请求动词必须是
GET
。我正在使用此代码段测试:

var dataSource = new kendo.data.DataSource({
        serverSorting: true,
        sort: [{ field: "ProductName", dir: "desc" },
               { field: "Category", dir: "asc"}],

        serverPaging: true,
        page: 2,
        pageSize: 5,

        transport: {

            read: {
                url: "/products",
                dataType: "json",
                contentType: "application/json",
                dataType: "jsonp"
            },

            parameterMap: function (data, type) {
                //return kendo.stringify(data);
                //return JSON.stringify(data);
                //return $.param(data, true);
                //data.sort = kendo.stringify(data.sort);
                return data;
            }
        }
    });

    dataSource.fetch(function () {
        console.log(dataSource.view());
    });
排序参数会变成锯齿状数组,如:

sort[0][field]: ProductName
sort[0][dir]: desc
sort[1][field]: Category
sort[1][dir]: asc
我的要求是:

    public class SortTerm
{
    public string field { get; set; }
    public string dir { get; set; }
}

public class KendoQuery
{

    public List<SortTerm> Sort { get; set; }

    public int Skip { get; set; }
    public int Take { get; set; }
    public int Page { get; set; }
    public int PageSize { get; set; }
}
公共类排序器
{
公共字符串字段{get;set;}
公共字符串dir{get;set;}
}
公共类KendoQuery
{
公共列表排序{get;set;}
公共整数跳过{get;set;}
公共整数Take{get;set;}
公共整型页{get;set;}
公共int PageSize{get;set;}
}
所有的简单参数都被反序列化,但我究竟如何转换
Sort
属性(客户端或服务器端)以使其正确填充

请注意,我在
parameterMap
函数中尝试了各种序列化技术,我完全被难倒了

编辑


因此,这一切归结为:当jQuery深思熟虑地将我的请求重新排列为锯齿状数组时,如何通过$.get()将对象数组传递给ServiceStack服务?这听起来像是一个请求过滤器,但我不得不想象它以前在
**GET**
请求中已经被解决了。

您不能使用复杂的数据结构,比如锯齿状排序数组,使用标准技术执行
GET
请求,因为JSON
GET
请求只支持简单变量,因为这些变量可以转换为查询字符串中的请求参数。因此,对于简单参数
?Page=2&PageSize=5…
,请求将正确形成,但对于
sort[0][field]
,这不能指定为查询字符串请求参数

但是,如果要使用字符串化搜索条件对象,以便将其作为参数传递,然后在服务器上转换,则可以解决此问题

parameterMap:函数(数据,类型){
//将搜索条件转换为JSON字符串,并将其存储在值sortJson上
data.sortJson=JSON.stringify(data.sort);
//删除排序值,因为这将由sortJson提供
删除数据。排序;
//返回数据
返回数据
}
在服务器上,您需要处理将JSON字符串转换为
列表的操作:

public类KendoQuery
{
列表排序;
公共列表排序{
得到{
//处理对SortJson值的反序列化
if(sort==null)
sort=ServiceStack.JsonSerializer.DeserializeFromString(SortJson);
返回排序;
}
}
公共字符串SortJson{get;set;}
公共整数跳过{get;set;}
公共整数Take{get;set;}
公共整型页{get;set;}
公共int PageSize{get;set;}
}

我希望这会有所帮助。

上面斯科特的答案是正确的,但这里是我的更以服务器为中心的解决方案。基本上,我从查询字符串手动重建
SortTerm
对象。这可以扩展到其他剑道特定参数,如过滤器和组

客户端代码:

<html>
<head>
    <meta charset="utf-8">
    <title>Kendo UI Snippet</title>

    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://cdn.kendostatic.com/2014.1.528/js/kendo.all.min.js"></script>
</head>
<body>
    <script>
        var dataSource = new kendo.data.DataSource({
            serverSorting: true,
            sort: [{ field: "ProductName", dir: "desc" },
                   { field: "Category", dir: "asc" }],

            serverPaging: true,
            page: 2,
            pageSize: 5,

            transport: {
                read: {
                    url: "/products",
                    dataType: "json"
                }
            }
        });

        dataSource.fetch(function () {
            console.log(dataSource.view());
        });
    </script>
</body>
</html>

剑道UI片段
var dataSource=new kendo.data.dataSource({
对,,
排序:[{field:“ProductName”,dir:“desc”},
{字段:“类别”,目录:“asc”},
对,,
页码:2,
页面大小:5,
运输:{
阅读:{
url:“/products”,
数据类型:“json”
}
}
});
dataSource.fetch(函数(){
log(dataSource.view());
});
服务器代码(在AppHost实现中)

this.GlobalRequestFilters.Add((req, resp, dto) =>
{
    if (dto is KendoQueryBase)
    {
        KendoQueryBase qb = dto as KendoQueryBase;
        if (qb.Sort == null) qb.Sort = new List<SortTerm>();
        Dictionary<string, string> qs = req.QueryString.ToDictionary();
        var i = 0;
        while (qs.ContainsKey("sort[{0}][field]".Fmt(i)))
        {
            qb.Sort.Add(new SortTerm()
            {
                field = qs["sort[{0}][field]".Fmt(i)],
                dir = qs["sort[{0}][dir]".Fmt(i)]
            });
            i++;
        }
    }
});
this.GlobalRequestFilters.Add((req,resp,dto)=>
{
如果(dto是KendoQueryBase)
{
KendoQueryBase qb=dto作为KendoQueryBase;
如果(qb.Sort==null)qb.Sort=new List();
Dictionary qs=req.QueryString.ToDictionary();
var i=0;
while(qs.ContainsKey(“sort[{0}][field]”.Fmt(i)))
{
qb.Sort.Add(新的SortTerm()
{
field=qs[“sort[{0}][field]”.Fmt(i)],
dir=qs[“排序[{0}][dir]”。Fmt(i)]
});
i++;
}
}
});

正如您所说,我之所以要退出,是因为GET无法合理地传递复杂的数据结构。虽然我说过客户端或服务器端修复都可以,但我更喜欢服务器端保持客户端代码干净,后端不可知。我已经在下面的答案中发布了我的代码。@jklemmack是的,尽可能保持客户端干净是合理的,因此您的解决方案看起来也很合适(+1)。