C# 使用JSON将数据传递给ASP.NETWebService
我想知道如何使用JSON将数据从jquery传递到web服务 我的意思是,如果数组的长度一直在动态变化,我应该使用什么样的数据类型作为Web服务的输入,例如路线序列号和路线的登机位置号,下面是一个示例C# 使用JSON将数据传递给ASP.NETWebService,c#,jquery,asp.net,ajax,json,C#,Jquery,Asp.net,Ajax,Json,我想知道如何使用JSON将数据从jquery传递到web服务 我的意思是,如果数组的长度一直在动态变化,我应该使用什么样的数据类型作为Web服务的输入,例如路线序列号和路线的登机位置号,下面是一个示例 { "route": [ { "serial": { "name": " ", "rsn": " ", "boardingzone": { "zone": [
{ "route": [
{
"serial": {
"name": " ",
"rsn": " ",
"boardingzone": {
"zone": [
{ "name": " ", "time": " ", "qouta": " " },
{ "name": " ", "time": " ", "qouta": " " },
{ "name": " ", "time": " ", "qouta": " " }
]
},
"destination": {
"zone": [
{ "name": " " },
{ "name": " " },
{ "name": " " }
]
}
}
}
] }
另外,我想知道asp.net需要什么样的格式,以便我可以相应地更正我的编码,提前感谢您的评论和回复。您可以创建支持JSON的WCF服务。以下是您的入门指南。您可以创建支持JSON的WCF服务。下面让您开始。我意识到这个问题是不久前提出的,在ASP.Net中有很多方法可以解决这个问题。我通常在aspx页面上使用WebMethods。您也可以使用asmx Web服务文件——Robert很好地解释了这一点 对于类似上面的结构,我在C#中使用泛型和结构,以便在类似的庄园中更轻松地处理服务器端的数据—数据是用JavaScript处理的。还可以更容易地序列化JSON。我意识到这样做有一些初始开销。我的目标是使在C语言中处理服务器端的数据与使用JavaScript处理前端的数据一样容易 除了在VS2010中自动添加的名称空间之外,我还引用了以下名称空间: 然后我定义以下结构:
public struct RouteAddedResponse {
public int? id;
public int status;
public string message;
}
public struct BoardingZoneDetail
{
public string name;
public string time;
public string quota;
}
public struct DestinationZoneDetail
{
public string name;
}
public struct RouteSerial
{
public string name;
public string rsn;
public Dictionary<string, List<BoardingZoneDetail>> boardingzone;
public Dictionary<string, List<DestinationZoneDetail>> destination;
}
公共结构RouteAddResponse{
公共int?id;
公众地位;
公共字符串消息;
}
公共结构BoardingZoneDetail
{
公共字符串名称;
公共字符串时间;
公共字符串配额;
}
公共结构目标分区详细信息
{
公共字符串名称;
}
公共结构路由序列
{
公共字符串名称;
公共字符串rsn;
公共词典寄宿区;
公共词典目的地;
}
下面是ScriptMethod的一个示例
// WebMethod expects: Dictionary<string, List<Dictionary<string, RoutSerial>>>;
// Change UseHttpGet to false to send data via HTTP GET.
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json, UseHttpGet = false)]
public static RouteAddedResponse AddRouteData(List<Dictionary<string, RouteSerial>> route)
{
// Iterate through the list...
foreach (Dictionary<string, RouteSerial> drs in route) {
foreach (KeyValuePair<string,RouteSerial> rs in drs)
{
// Process the routes & data here..
// Route Key:
// rs.Key;
// Route Data/Value:
// rs.Value;
// ...
}
}
return new RouteAddedResponse() { id = -1, status = 0, message = "your message here" };
}
//WebMethod需要:字典;
//将UseHttpGet更改为false以通过HTTP GET发送数据。
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod(ResponseFormat=System.Web.Script.Services.ResponseFormat.Json,UseHttpGet=false)]
公共静态RouteAddResponse AddRouteData(列表路由)
{
//遍历列表。。。
foreach(路由中的字典drs){
foreach(drs中的密钥估价师对)
{
//在此处处理路由和数据。。
//路线键:
//rs.Key;
//路线数据/值:
//卢比价值;
// ...
}
}
返回新的RouteAddResponse(){id=-1,status=0,message=“此处的消息”};
}
脚本方法,AddRouteData
,期望通过HTTPPOST获得上述结构。如果要使用GET请求,则方法参数将是查询字符串变量
注意事项
在ASP.Net中使用ScriptMethods时,您需要确保Content-Type
标题设置为:application/json;charset=utf-8
无论您使用的是GET还是POST请求
希望有帮助 我意识到这个问题是很久以前提出的,在ASP.Net中有很多方法可以解决这个问题。我通常在aspx页面上使用WebMethods。您也可以使用asmx Web服务文件——Robert很好地解释了这一点 对于类似上面的结构,我在C#中使用泛型和结构,以便在类似的庄园中更轻松地处理服务器端的数据—数据是用JavaScript处理的。还可以更容易地序列化JSON。我意识到这样做有一些初始开销。我的目标是使在C语言中处理服务器端的数据与使用JavaScript处理前端的数据一样容易 除了在VS2010中自动添加的名称空间之外,我还引用了以下名称空间: 然后我定义以下结构:
public struct RouteAddedResponse {
public int? id;
public int status;
public string message;
}
public struct BoardingZoneDetail
{
public string name;
public string time;
public string quota;
}
public struct DestinationZoneDetail
{
public string name;
}
public struct RouteSerial
{
public string name;
public string rsn;
public Dictionary<string, List<BoardingZoneDetail>> boardingzone;
public Dictionary<string, List<DestinationZoneDetail>> destination;
}
公共结构RouteAddResponse{
公共int?id;
公众地位;
公共字符串消息;
}
公共结构BoardingZoneDetail
{
公共字符串名称;
公共字符串时间;
公共字符串配额;
}
公共结构目标分区详细信息
{
公共字符串名称;
}
公共结构路由序列
{
公共字符串名称;
公共字符串rsn;
公共词典寄宿区;
公共词典目的地;
}
下面是ScriptMethod的一个示例
// WebMethod expects: Dictionary<string, List<Dictionary<string, RoutSerial>>>;
// Change UseHttpGet to false to send data via HTTP GET.
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json, UseHttpGet = false)]
public static RouteAddedResponse AddRouteData(List<Dictionary<string, RouteSerial>> route)
{
// Iterate through the list...
foreach (Dictionary<string, RouteSerial> drs in route) {
foreach (KeyValuePair<string,RouteSerial> rs in drs)
{
// Process the routes & data here..
// Route Key:
// rs.Key;
// Route Data/Value:
// rs.Value;
// ...
}
}
return new RouteAddedResponse() { id = -1, status = 0, message = "your message here" };
}
//WebMethod需要:字典;
//将UseHttpGet更改为false以通过HTTP GET发送数据。
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod(ResponseFormat=System.Web.Script.Services.ResponseFormat.Json,UseHttpGet=false)]
公共静态RouteAddResponse AddRouteData(列表路由)
{
//遍历列表。。。
foreach(路由中的字典drs){
foreach(drs中的密钥估价师对)
{
//在此处处理路由和数据。。
//路线键:
//rs.Key;
//路线数据/值:
//卢比价值;
// ...
}
}
返回新的RouteAddResponse(){id=-1,status=0,message=“此处的消息”};
}
脚本方法,AddRouteData
,期望通过HTTPPOST获得上述结构。如果要使用GET请求,则方法参数将是查询字符串变量
注意事项
在ASP.Net中使用ScriptMethods时,您需要确保Content-Type
标题设置为:application/json;charset=utf-8
无论您使用的是GET还是POST请求
希望有帮助 很好地解释了这一点,但我使用Json.NET库进行管理,因为我的学校沙盒服务器不允许我安装