【问题标题】:How to return a multidimensional array in jquery ajax call如何在jquery ajax调用中返回多维数组
【发布时间】:2011-11-03 20:47:30
【问题描述】:

我正在调用 webmethod 以在 jquery 中返回字符串数组。常规的一维字符串数组是这样的

[WebMethod]
public static string[] GetNewRow() {
    var listOfItems = new List<string>();
    // populate the listOfItems here
    listOfItems.Add("100");
    // more here
    return listOfItems.ToArray();
}

是否可以返回多维数组?

【问题讨论】:

  • 你能展示什么结构是理想的吗?真的只是一个 [][] 吗?
  • jquery 可接受的任何结构。我尝试返回多维字符串 (string[,]),但在 jquery 中它被视为一维数组。如果可能的话,我想用两个参数 [a][b] 来引用它

标签: jquery multidimensional-array


【解决方案1】:

在 javascript 中,数组 ( [] ) 是数字索引的。您必须使用对象 ( {} ) 通过命名索引来访问它。以下示例在 javascript 中有效

var thing = { a: {'b': 10}};
thing.a['b'] = thing['a'].b + thing['a']['b'] * thing.a.b;

您可能会获得有关 JSON 对象的信息,以及服务器端语言上的函数“object_to_json”。检查this link for java,或json_encode 用于php。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多