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

发布于 2025-01-03 06:26:52 字数 1703 浏览 0 评论 0原文

这不是最清晰的问题标题,对此感到抱歉,但我会尽力分解我的问题。

我有一些客户端 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 对象,那么它会反序列化为适当的服务器端对象(已定义所有键)。

//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
}   

我的问题是我试图传递一个对象,其中的键并不全部已知 - 一个简单的键/值对列表。但我无法让它正确反序列化。使用“字典”作为服务器端类型不起作用,通用“对象”也不起作用。

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

非常感谢。

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. I am serialising this data using 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).

//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? Is what I am trying to achieve possible?

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

定格我的天空 2025-01-10 06:26:52

@Etch 的答案是正确的,但如果我没记错的话,KeyValuePair 不太适合序列化。
你可以定义一个你自己的简单的 (key, value) 结构,并传递它:

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's answer is on the right track, but if I remember correctly, KeyValuePair isn't very serialization-friendly.
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:
[ {'Key' : 'key 1', 'Value': 'value 1'}, {'Key' : 'key 2', 'Value': 'value 2'}]

蓝礼 2025-01-10 06:26:52

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

"requestData":[{"Key1":"Val1"},{"Key2":"Val2"}]

则应与 KeyValuePair 配合使用。我没有尝试过使用KeyValuePair,但我以这种方式使用List<>

编辑

查看我的服务后,我注意到我不使用List<>,但我使用数组。所以在你的情况下:

public string ajaxCall(Person[] requestData){
}

If you change your json for calling the service to

"requestData":[{"Key1":"Val1"},{"Key2":"Val2"}]

That should work with KeyValuePair<string,string>. I havent tried to use KeyValuePair but I use List<> in this fashion.

Edit

After looking over my services I noticed I do not use List<> however I use an array. so in your case:

public string ajaxCall(Person[] requestData){
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文