簡體   English   中英

使用JSON將數據傳遞到ASP.Net Web服務

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

我想知道如何使用JSON將數據從jquery傳遞到Web服務?

我的意思是,如果數組的長度一直在動態變化,例如路由序列號和路由登機位置數,那么我應該使用哪種數據類型作為Web服務的輸入,以下是示例之一。

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

我也想知道asp.net希望使用哪種格式,以便我可以相應地更正編碼,在此先感謝您的任何評論和答復。

您可以創建啟用JSON的WCF服務。 這是簡單的入門指南。

我意識到這個問題是在不久前提出的,並且在ASP.Net中有很多方法可以解決此問題。 我通常要做的是在aspx頁面上使用WebMethods。 您也可以使用asmx Web服務文件-Robert 在這里做了很好的解釋。

對於上面的結構,我使用C#中的泛型和結構來簡化以類似的方式在服務器端處理數據的過程,這些數據是用JavaScript處理的。 還使序列化JSON更容易。 我意識到以這種方式進行操作會產生一些初始開銷。 我的目標是使在C#中像在JavaScript前端那樣在服務器端使用數據變得容易。

除了在VS2010中自動添加的 名稱空間 之外, 我還引用了以下名稱空間

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

然后,我定義以下結構:

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

以下是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" };
}

腳本方法AddRouteData期望通過HTTP POST獲得上面概述的結構。 如果要使用GET請求,則方法參數將是查詢字符串變量。

注意事項

在ASP.Net中使用ScriptMethods時,需要確保Content-Type標頭設置為: application/json; charset=utf-8 application/json; charset=utf-8無論您使用的是GET還是POST請求。

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM