[英]What is the server side type of a javascript object, serialised client side using JSON, that is passed via an ajax call?
This is not the clearest of question titles, sorry about that, but I will try and break down my issue as best I can. 抱歉,这不是最清晰的问题标题,但我会尽力解决我的问题。
I have some client side javascript, and am trying to make an ajax server side call, passing through some data. 我有一些客户端JavaScript,并且正在尝试通过一些数据进行ajax服务器端调用。 I am serialising this data using JSON.
我正在使用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)
}
This is the code I am using, which works fine. 这是我正在使用的代码,效果很好。
//client side JS
var dataToStringify = 'John'
//Becomes- '{"requestData":"John Smith"}'
//server side c#
[WebInvoke(Method = "POST")]
public string ajaxCall(string requestData){
//stuff
}
If I serialise a simple string, it deserialises to a string. 如果我序列化一个简单的字符串,它将反序列化为一个字符串。
//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
}
If I serialise a javascript object, then it deserialises to an appropriate server side object (has all keys defined). 如果我序列化一个javascript对象,则它将反序列化为适当的服务器端对象(已定义了所有键)。
//client side JS
var dataToStringify = {}
dataToStringify.Key1 = 'Val1'
dataToStringify.Key2 = 'Val2'
...
dataToStringify.Keyn = 'Valn'
//Becomes- '{"requestData":{"Key1":"Val1","Key2":"Val2"...,,"Keyn":"Valn"}}'
//server side c#
[WebInvoke(Method = "POST")]
public string ajaxCall(??? requestData){
//stuff
}
My problem is that I am trying to pass an object where the keys are not all known - a simple list of key/value pairs. 我的问题是我试图传递一个键不完全知道的对象-键/值对的简单列表。 But I cannot get it to deserialise correctly.
但是我无法正确反序列化。 Using 'Dictonary' as the server side type does not work, nor does a generic 'Object'.
使用“字典”作为服务器端类型不起作用,通用“对象”也不起作用。
In my last example, what type should the reqestData object be? 在我的最后一个示例中,reqestData对象应为哪种类型? Is what I am trying to achieve possible?
我试图实现的目标有可能吗?
Many thanks. 非常感谢。
@Etch's answer is on the right track, but if I remember correctly, KeyValuePair
isn't very serialization-friendly. @Etch的答案是正确的,但是如果我没记错的话,
KeyValuePair
并不是非常易于序列化的。
you can define a simple (key, value) struct of your own, and pass that: 您可以定义自己的简单(键,值)结构,然后传递该结构:
struct JSONableKeyValuePair
{
public string Key;
public string /*or whatever type*/ Value;
}
and then your json should be an array of those, looking something like: 然后您的json应该是这些数组的数组,看起来像:
[ {'Key' : 'key 1', 'Value': 'value 1'}, {'Key' : 'key 2', 'Value': 'value 2'}]
If you change your json for calling the service to 如果您更改用于调用服务的json
"requestData":[{"Key1":"Val1"},{"Key2":"Val2"}]
That should work with KeyValuePair<string,string>
. 那应该与
KeyValuePair<string,string>
。 I havent tried to use KeyValuePair
but I use List<>
in this fashion. 我没有尝试使用
KeyValuePair
但我以这种方式使用List<>
。
Edit 编辑
After looking over my services I noticed I do not use List<>
however I use an array. 查看服务后,我发现我不使用
List<>
但是我使用数组。 so in your case: 所以在你的情况下:
public string ajaxCall(Person[] requestData){
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.