4

我很高兴使用 javascript 对象序列化为 JSON

         JSON.stringify

并在 c#/asp.net 中发送到我的“静态”webmethod 并确定它到达.. 我需要正确数量的参数,因此如果我的 json 对象具有“startDate”、“endDate”、“reserve”,那么我的 webmethod 需要这些作为参数。

“基本上,对于我拥有的订单对象,我在这个对象上有许多参数,所以我需要在 webmethod 上使用相同的数字 - 这有点乱??” - 我会解释

我在 javascript 中有一个相当复杂的“Order”对象,并希望使用 stringify 对其进行序列化并将其发送到我的 webmethod 但我不想指定所有参数有没有办法解决这个问题?

我希望在我的 webmethod 上有这样的东西

           public static bool MakeReservation(object order)

然后在我的 webmethod 中,我只有 1 个参数,但我可以使用 JSON.NET 将其反串化为真正的 c# 对象。我已经尝试过像这样发送json,但是因为我的webmethod上只有1个参数它失败了。

基本上我想说的是,如果我想继续使用我的 webmethod 但我不想在 webmethod 上指定 15 个参数

我希望 JSON - 字符串到达​​我的网络方法,然后我可以在服务器上分解它。

这可能吗?

这是我目前使用 jquery 将我的 JSON 发送到服务器(webmethod)的方式

    var jsonData = JSONNew.stringify(orderObject);

    $.ajax({
        type: "POST",
        url: "MyService.aspx/DoReservation",
        data: jsonData,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            success = true;
        },
        error: function(msg) {
            success = false;
        },
        async: false
    });
4

3 回答 3

9

如果您尝试提交如下所示的对象:

JSON.stringify({ endDate: new Date(2009, 10, 10), startDate: new Date() });

它会尝试将 endDate 和 startDate 映射到您的 web 方法中的相应参数。由于您只想接受一种方法,我怀疑您可以通过提交以下内容来摆脱它:

JSON.stringify({ order: orderObject });

它可能会合理地尝试将其作为值分配给您的网络方法的“订单”参数。做不到,提交

JSON.stringify({ order: JSON.stringify(orderObject) });

然后使用 JSON.NET 反序列化它肯定可以工作,但它更丑陋,所以先尝试第一个示例。那是我最好的投篮。

于 2009-07-06T17:27:35.087 回答
4

这是可能的。我不太擅长解释东西,我将向您展示我的示例代码:

Javascript:

var Order = function(name, orderid, price) {
this.name = name;
this.orderid = orderid;
this.price = price;}

var pagePath = window.location.pathname;

function setOrder() {
var jsOrder = new Order("Smith", 32, 150);
var jsonText = JSON.stringify({ order: jsOrder });
$.ajax({
    type: "POST",
    url: pagePath + "/SetOrder",
    contentType: "application/json; charset=utf-8",
    data: jsonText,
    dataType: "json",
    success: function(response) {
        alert("wohoo");
    },
    error: function(msg) { alert(msg); }
});
}

后面的 C# 代码

public class Order
{
 public string name { get; set; }
 public int orderid { get; set; }
 public int price { get; set; }
}

[WebMethod]
public static void SetOrder(object order)
{
    Order ord = GetOrder(order);
    Console.WriteLine(ord.name +","+ord.price+","+ord.orderid);        
}
public static Order GetOrder(object order)
{
    Order ord = new Order();
    Dictionary<string,object> tmp = (Dictionary<string,object>) order;
    object name = null;
    object price = null;
    object orderid = null;
    tmp.TryGetValue("name", out name);
    tmp.TryGetValue("price", out price);
    tmp.TryGetValue("orderid", out orderid);
    ord.name = name.ToString();
    ord.price = (int)price;
    ord.orderid = (int) orderid;
    return ord;
}

我的代码不是那么漂亮,但我希望你能理解它的含义。

于 2009-08-03T18:55:39.553 回答
1

I recently went through this very issue with a complexe JSON object I wanted to deserilize in my controller. The only difference is I was using the .toJSON plugin on the client instead of .stringify.

The easy answer:

public static bool MakeReservation(string orderJSON)
{
    var serializer = new JavaScriptSerializer();
    var order = serializer.Deserialize<Order>(orderJSON);
}

Now, as a longer term solution we created a custom ActionFilterAttribute to handle these cases. It detects the JSON parameters and handles deserilizing the object and mapping to the action. You may want to look into doing the same.

于 2009-08-03T19:07:16.627 回答