Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Javascript多维数组强制转换为ASMX方法_C#_Asp.net_Ajax - Fatal编程技术网

C# 将Javascript多维数组强制转换为ASMX方法

C# 将Javascript多维数组强制转换为ASMX方法,c#,asp.net,ajax,C#,Asp.net,Ajax,我正在尝试以以下形式传递多层数组: array[<string>, array[<string>, <string>]] array[,array[,] 使用jqueryajax var cssarray = []; function SaveChanges(element, updates) { var exists = false; var updated = false; //need to ch

我正在尝试以以下形式传递多层数组:

array[<string>, array[<string>, <string>]]
array[,array[,]
使用jqueryajax

var cssarray = [];

function SaveChanges(element, updates) {

        var exists = false;
        var updated = false;

        //need to check if already present and if so overwrite the changes.
        for (var i = 0; i < cssarray.length; i++) {

            if (cssarray[i][0] === element) {
                exists = true;

                var existingupdates = cssarray[i][1];

                for (var j = 0; j < updates.length; j++) {

                    for (var k = 0; k < existingupdates.length; k++) {

                        if (existingupdates[k][0] === updates[j][0]) {
                            existingupdates[k][1] = updates[j][1];
                            updated = true;
                        }
                    }

                    if (!updated)
                       existingupdates.push(updates[j]); 
                }

                cssarray[i][1] = existingupdates;
            }
        }

        if(!exists)
            cssarray.push([element, updates]);
    }


runAjax('/Public/AdminServices.asmx/UpdateCSS', 'updates', cssarray)
        .done(
            function (__exists) {

                if (__exists.d) {
                    alert(__exists.d);
                }
                else {
                    alert("hmmm error");
                    alert(__exists.d);
                }
            }
        )
        .fail(
            function (jqXHR, textStatus, errorThrown) {
                alert(errorThrown + ':' + textStatus + ':' + jqXHR.responseText);
            }
        );

function runAjax(webmethod, fieldname, fieldtext) {

    var jsonObject = {};
    jsonObject[fieldname] = fieldtext;

    return $.ajax({
        type: "POST",
        url: webmethod,
        data: JSON.stringify(jsonObject),
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}
var cssarray=[];
函数保存更改(元素、更新){
var=false;
var=false;
//需要检查是否已经存在,如果已经存在,则覆盖更改。
对于(var i=0;i
我想把它传递给我的asmx方法

[WebMethod(EnableSession = true)]
[SoapHeader("authentication")]
public string UpdateCSS(string updates)
{

    //add is master check so only admin will be able to change the website
    if (User.Identity.IsAuthenticated &&
        !string.IsNullOrEmpty(Convert.ToString(Session["IsMaster"])))
    {
        JavaScriptSerializer json = new JavaScriptSerializer();
        List<KeyValuePair<String, List<KeyValuePair<String, String>>>> css = json.Deserialize<List<KeyValuePair<String, List<KeyValuePair<String, String>>>>>(updates);
       AdminFunctions.UpdateStyleSheet(css);
        return "User is a valid user";
    }
    else
    {
        return "changes not committed as the user is not authenticated.";
    }
}
[WebMethod(EnableSession=true)]
[SoapHeader(“身份验证”)]
公共字符串更新CSS(字符串更新)
{
//添加是主检查,因此只有管理员才能更改网站
如果(User.Identity.i)已验证&&
!string.IsNullOrEmpty(Convert.ToString(会话[“IsMaster”]))
{
JavaScriptSerializer json=新的JavaScriptSerializer();
列表css=json.反序列化(更新);
AdminFunctions.UpdateStyleSheet(css);
返回“用户是有效用户”;
}
其他的
{
return“未提交更改,因为用户未经过身份验证。”;
}
}
但是它在这个对象上抛出了一个无法投射的错误?抛出的错误如下所示

反序列化不支持“Type”System.String 数组的“,”StackTrace“:”位于 System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList 列表,类型,JavaScriptSerializer序列化程序,布尔值 throwOnError,IList\u0026 convertedList)\r\n位于 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(对象 o、 类型类型,JavaScriptSerializer序列化程序,布尔错误, 对象\u0026 convertedObject)\r\n位于 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(对象 o、 类型类型,JavaScriptSerializer序列化程序,布尔错误, 对象\u0026 convertedObject)\r\n位于 System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary
2
rawParams)\r\n位于
System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext
上下文,WebServiceMethodData methodData,IDictionary
2个rawParams)\r\n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext 上下文,WebServiceMethodData methodData)”,“ExceptionType”:“System.InvalidOperationException”}

你知道为什么吗


提前谢谢。

我会用字典>不久前我做了类似的事情。或者尝试嵌套的NameValueCollection

改变

JavaScriptSerializer json = new JavaScriptSerializer();
List<KeyValuePair<String, List<KeyValuePair<String, String>>>> css = json.Deserialize<List<KeyValuePair<String, List<KeyValuePair<String, String>>>>>(updates);
JavaScriptSerializer json=新的JavaScriptSerializer();
列表css=json.反序列化(更新);

JavaScriptSerializer json=新的JavaScriptSerializer();
字典css=json.反序列化(更新);

好的,问题是我需要对子数组进行字符串化,因此下面的代码修复了脚本端的问题


JSON.stringify({updates:JSON.stringify(fieldtext)})

Matt,这很好,但问题在于服务器端抛出了错误。我尝试了
JavaScriptSerializer json=newJavaScriptSerializer();列表css=json.反序列化(更新)此位在施法时也会引发错误?您能否提供一份在强制转换时使用的工作代码的副本?您需要更新您的问题,以获得例外情况
JavaScriptSerializer json = new JavaScriptSerializer();
Dictionary<String, Dictionary<String, String>> css = json.Deserialize<Dictionary<String, Dictionary<String, String>>>(updates);