简体   繁体   English

使用JSON将数据传递到ASP.Net Web服务

[英]Passing data to ASP.Net webservice with JSON

I would like to know how could I use JSON for passing data from jquery to a web-services? 我想知道如何使用JSON将数据从jquery传递到Web服务?

I mean what kind of datatype should I use as input for the webservice if the length of the array are dynamically changed all the time for example the number of route serial and number of boarding location for a route, below is one of the example. 我的意思是,如果数组的长度一直在动态变化,例如路由序列号和路由登机位置数,那么我应该使用哪种数据类型作为Web服务的输入,以下是示例之一。

{ "route": [
    {
        "serial": {
        "name": " ",
        "rsn": " ",
        "boardingzone": {
                            "zone": [
                                { "name": " ",   "time": " ",   "qouta": " "   },
                                { "name": " ",   "time": " ",   "qouta": " "   },
                                { "name": " ",   "time": " ",   "qouta": " "   }
                                    ]
            },
        "destination": {
                            "zone": [
                                { "name": " " },
                                { "name": " " },
                                { "name": " " }
                        ]
            }
    }
}
] }

Also I would like to know what kind of format is asp.net expecting, so that I could correct my coding accordingly , thank you in advance for any comment and reply. 我也想知道asp.net希望使用哪种格式,以便我可以相应地更正编码,在此先感谢您的任何评论和答复。

You can create JSON enabled WCF services. 您可以创建启用JSON的WCF服务。 Here is simple tutorial to get you started. 这是简单的入门指南。

I realize this question was asked some time ago, and that there are a plethora of ways to solve this in ASP.Net. 我意识到这个问题是在不久前提出的,并且在ASP.Net中有很多方法可以解决此问题。 What I've typically done is use WebMethods on aspx pages. 我通常要做的是在aspx页面上使用WebMethods。 You can also use an asmx Web Services file too - Robert does a good job explaining that here . 您也可以使用asmx Web服务文件-Robert 在这里做了很好的解释。

For something like the structure above I use generics and structs in C# to make it easier to handle the data on the server-side in a similar manor the data is handled in JavaScript. 对于上面的结构,我使用C#中的泛型和结构来简化以类似的方式在服务器端处理数据的过程,这些数据是用JavaScript处理的。 Also makes it easier to serialize the JSON. 还使序列化JSON更容易。 I realize there is some initial overhead to doing it this way. 我意识到以这种方式进行操作会产生一些初始开销。 My goal would be to make it as easy in C# to work with the data on the server-side as it is on the front-end with JavaScript. 我的目标是使在C#中像在JavaScript前端那样在服务器端使用数据变得容易。

I Reference the following namespaces In addition to those added automatically in VS2010: 除了在VS2010中自动添加的 名称空间 之外, 我还引用了以下名称空间

using System.Collections;
using System.Web.Services;
using System.Web.Script;
using System.Web.Script.Serialization;
using System.Web.Script.Services;

I then define the following structs: 然后,我定义以下结构:

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

Below is an example of the ScriptMethod 以下是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" };
}

The script method, AddRouteData , is expecting the structure outlined above via HTTP POST. 脚本方法AddRouteData期望通过HTTP POST获得上面概述的结构。 If you would be using sing a GET request the method arguments would be query string variables. 如果要使用GET请求,则方法参数将是查询字符串变量。

Caveats 注意事项

In using ScriptMethods with ASP.Net, you need to be sure that the Content-Type header is set to: application/json; charset=utf-8 在ASP.Net中使用ScriptMethods时,需要确保Content-Type标头设置为: application/json; charset=utf-8 application/json; charset=utf-8 whether you are using a GET or POST request. application/json; charset=utf-8无论您使用的是GET还是POST请求。

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM