将String [] []发送到Web服务

时间:2012-11-20 14:31:18

标签: c# jquery ajax

需要通过对Web服务的AJAX调用将Sqlite表的值发送到服务器。我无法弄清楚如何以Web服务接受它的方式打包此表的行。

网络方法签名

[WebMethod]
    public void CVW(string[][] qAndA)
    {}

客户端

var qAndA = new Array();
tx.executeSql("Select * From CVW Where CallNumber=?", [call], function (tx, result) {
                        var i = 0;
                        $.each(result.rows, function () {
                            qAndA[i] = result.rows.item(i);
                            i++;
                        });
$.ajax({
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json",
                            url: "Service.asmx/CVW",
                            data: JSON.stringify({ qAndA: qAndA }),
                            success: function (data) {
                            },
                            error: function (xhr, status, error) {
                                var err = eval("(" + xhr.responseText + ")");
                                alert(err.Message);
                            }
                        });
                    }, onSQLError);

这给了我No parameterless constructor defined for type System.String。我已经尝试将web方法参数更改为string []和List以及相同的东西。

当我查看Fire Bug中发送的内容时,我发现它正在Object[object Object][object Object][object Object]...[object Object]的形式发送,因为Sqlite查询中返回的行数为data: JSON.stringify({ qAndA: qAndA }), 。 C#中我可以用作参数类型的相应类型是什么?

编辑:最终解决方案

在javascript中我不得不改变

var json = JSON.stringify(qAndA);
data: JSON.stringify({ qAndA: json });

 [WebMethod]
    public void CVW(string qAndA)

JavaScriptSerializer ser = new JavaScriptSerializer();
List<CVWQuestion> list = ser.Deserialize<List<CVWQuestion>>(qAndA);

[Serializable]
public class CVWQuestion
{
    public string CallNumber {get;set;}
    public string Question { get; set; }
    public string P1 { get; set; }
    public string P2 { get; set; }
    public string P3 { get; set; }
    public string P4 { get; set; }
    public string P5 { get; set; }
    public string P6 { get; set; }
}

并因此改变了Web方法

{{1}}

3 个答案:

答案 0 :(得分:0)

请查看我编辑的帖子,了解最终适用于我的代码。在很大程度上发现了@SteveB,但从未提交过答案,所以我无法接受。

答案 1 :(得分:-1)

将String [] []参数更改为String。数据只是一个字符串发送。您需要将它从字符串化的JSON对象转换为可以解析的对象。查看JObject

从字符串化的JSON转换看起来像......

JObject.parse(myJSONString)

我会将web服务更改为

public void CVW(string qAndAStringJson){

   Object qAndAObj = JObject.parse(qAndAStringJson);
   string someVar = qAndAObj["someStringIndex"];

Object qAndAObj = JObject.parse(qAndAStringJson); string someVar = qAndAObj["someStringIndex"];

查看有关Stringify here的文档。您对Stringify的调用是将数据更改为表示JSON对象的字符串。

我从链接中拿到了这个。

}

注意它只是一个长字符串。

因此,如果您在网络服务方法中的{{x“:5,”y“:6}'上拨打JSON.stringify({x: 5, y: 6}); // '{"x":5,"y":6}' or '{"y":6,"x":5}',您可以拨打电话

JObject.parse()

string obj = JOBject.Parse('{"x":5,"y":6}');

答案 2 :(得分:-1)

您可以按以下格式传递二维数组

{
    "values[0][0]": "Some value",
    "values[1][0]": "Some value",
    "values[0][1]": "Some value",
    "values[1][2]": "Some value",
    "values[2][0]": "Some value",
    "values[0][2]": "Some value",
    "values[2][3]": "Some value",
    ...
}

或者,作为提到的点,使用JSON.stringify

另外,请查看此帖子Parse JSON in C#,这可能对您有所帮助。