Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.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/8/perl/9.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# javascript对象的服务器端类型是什么,使用JSON序列化客户端,通过ajax调用传递?_C#_Javascript_Ajax_Json_Serialization - Fatal编程技术网

C# javascript对象的服务器端类型是什么,使用JSON序列化客户端,通过ajax调用传递?

C# javascript对象的服务器端类型是什么,使用JSON序列化客户端,通过ajax调用传递?,c#,javascript,ajax,json,serialization,C#,Javascript,Ajax,Json,Serialization,这不是最清晰的问题标题,很抱歉,但我会尽我所能将我的问题分解 我有一些客户端javascript,正在尝试进行ajax服务器端调用,传递一些数据。我正在使用JSON序列化这些数据 $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", data: stringifyData(dataToStringify) dataType: "json", }); stringifyData

这不是最清晰的问题标题,很抱歉,但我会尽我所能将我的问题分解

我有一些客户端javascript,正在尝试进行ajax服务器端调用,传递一些数据。我正在使用JSON序列化这些数据

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: stringifyData(dataToStringify)
    dataType: "json",
});

stringifyData = function (dataToStringify){
    var requestParameters = {};
    requestParameters.requestData = dataToStringify;
    return JSON.stringify(requestParameters)
}
这是我正在使用的代码,运行良好

//client side JS
var dataToStringify = 'John'
//Becomes- '{"requestData":"John Smith"}'

//server side c#
[WebInvoke(Method = "POST")]
public string ajaxCall(string requestData){
    //stuff
}
如果我序列化一个简单字符串,它将反序列化为一个字符串

//client side JS
var dataToStringify = {}
dataToStringify.FirstName = 'John'
dataToStringify.SurName = 'Smith'
//Becomes- '{"requestData":{"Firstname":"John","Surname":"Smith"}}'

//server side c#
public class Person {
    string Firstname {get;set;}
    string Surname {get;set;}
}

[WebInvoke(Method = "POST")]
public string ajaxCall(Person requestData){
    //stuff
}
如果我序列化一个javascript对象,那么它将反序列化到一个适当的服务器端对象(定义了所有键)

我的问题是,我试图传递一个键并不都已知的对象——一个简单的键/值对列表。但我无法使它正确地反序列化。使用“dictional”作为服务器端类型不起作用,泛型“Object”也不起作用

在我的最后一个示例中,reqestData对象应该是什么类型?我正在努力实现的目标可能吗


非常感谢。

如果您将调用服务的json更改为

"requestData":[{"Key1":"Val1"},{"Key2":"Val2"}]
这应该适用于
KeyValuePair
。我没有尝试使用
KeyValuePair
,但我以这种方式使用
List

编辑

查看我的服务后,我注意到我没有使用
List
,而是使用了数组。因此,在你的情况下:

public string ajaxCall(Person[] requestData){
}

如果更改调用服务的json,则

"requestData":[{"Key1":"Val1"},{"Key2":"Val2"}]
这应该适用于
KeyValuePair
。我没有尝试使用
KeyValuePair
,但我以这种方式使用
List

编辑

查看我的服务后,我注意到我没有使用
List
,而是使用了数组。因此,在你的情况下:

public string ajaxCall(Person[] requestData){
}

@Etch的答案是正确的,但如果我没记错的话,
KeyValuePair
对序列化不是很友好。
您可以定义自己的简单(键、值)结构,并传递:

struct JSONableKeyValuePair
{
   public string Key;
   public string /*or whatever type*/ Value;
}
然后你的json应该是一个数组,看起来像:

[{'Key':'Key 1','Value':'Value 1'},{'Key':'Key 2','Value':'Value 2'}]

@Etch的答案是正确的,但如果我没记错的话,
KeyValuePair
对序列化不是很友好。
您可以定义自己的简单(键、值)结构,并传递:

struct JSONableKeyValuePair
{
   public string Key;
   public string /*or whatever type*/ Value;
}
然后你的json应该是一个数组,看起来像:

[{'Key':'Key 1','Value':'Value 1'},{'Key':'Key 2','Value':'Value 2'}]

JavaScriptSerializer
将数据精确反序列化到
字典中。但是我不知道如何强制服务器端代码使用它,而不是
DataContractJsonSerializer
JavaScriptSerializer
将数据反序列化到
字典。但是我不知道如何强制服务器端代码使用它,而不是
DataContractJsonSerializer
,这太棒了。需要对我的key/value javascript对象进行一些重新调整,以获得正确的格式,现在我的服务器端代码接受我定义的结构数组。这太棒了。需要对我的key/value javascript对象进行一些重新调整,以获得正确的格式,现在我的服务器端代码接受我定义的结构的数组。