jQuery/ASMX:在 C# 中反序列化 JSON 时出错

Posted

技术标签:

【中文标题】jQuery/ASMX:在 C# 中反序列化 JSON 时出错【英文标题】:jQuery/ASMX: Error Deserializing JSON in C# 【发布时间】:2016-06-22 14:27:31 【问题描述】:

我正在针对 C# ASMX Web 方法执行 AJAX POST,但我无法获取 JSON 数据来反序列化。这应该可以,但是给了我以下错误...

"Message":"Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Collections.Generic.IDictionary`2[System.String,System.Object]\u0027","StackTrace":" at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, javascriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"

这里是 WebMethod...

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void SaveProfileProperties(string data)
    
        ups.Credentials = System.Net.CredentialCache.DefaultCredentials;
        try
        
            System.IO.File.WriteAllText(@"E:\Log.txt", data);
        
        catch  

        var json = new JavaScriptSerializer();
        Dictionary<string, string> props = json.Deserialize<Dictionary<string, string>>(data);
        SaveSQLData(props);
    

这是正在发送的数据...

"data:\"userName\":\"Doe\",\"UserProfile_GUID\":\"\",\"AccountName\":\"Domain\\Doe\",\"FirstName\":\"John\",\"LastName\":\"Doe\",\"PreferredName\":\"John Doe\",\"WorkPhone\":\"2176\",\"Department\":\"PHI\",\"Title\":\"Director\",\"SPS-JobTitle\":\"Director\",\"Manager\":\"Domain\\jdoe\",\"PersonalSpace\":\"/my/personal/Doe/\",\"PictureURL\":\"Doe_MThumb.jpg\",\"UserName\":\"Doe\",\"SPS-ClaimProviderID\":\"Windows\",\"SPS-ClaimProviderType\":\"Windows\",\"SPS-SavedAccountName\":\"Domain\\Doe\",\"SPS-DistinguishedName\":\"CN=John Doe,CN=Users,DC=domain,DC=local\",\"SPS-LastKeywordAdded\":\"1/2/2014 12:00:00 AM\",\"WorkEmail\":\"Doe@domain.com\",\"CellPhone\":\"5555555555\",\"Office\":\"C28\",\"SPS-TimeZone\":\"WF_Service.UPS.SPTimeZone\",\"personalEmail\":\"Doe.John@gmail.com\",\"SPS-EmailOptin\":\"0\",\"empID\":\"224\",\"team\":\"A\",\"p_id\":\"224\",\"username\":\"Doe\",\"last_name\":\"Doe\",\"first_name\":\"John\",\"birthday\":\"1/2/1985 12:00:00 AM\",\"anniv\":\"6/4/2007 12:00:00 AM\",\"imageurl\":\"A0BB.jpg\",\"Position\":\"Director\",\"dept_started_in\":\"IT\",\"birthplace\":\"NC\",\"marital_status\":\"Married\",\"sports_team\":\"Sporty Sports\",\"last_school\":\"None\",\"hobbies\":\"\",\"pets\":\"\",\"prior_employer\":\"None\",\"resides_in\":\"USA\",\"quote\":\"“Where you live should not determine whether you live, or whether you die.”  -- Bono\",\"fav_color\":\"009933\""

更新:这是客户端。经过一些测试,我发现我的帖子开头的堆栈跟踪错误实际上是客户端。

更新 2: 结果表明 JSON 序列化在客户端失败。这是准备数据的正确方法...

json.Stringify(data:\"userName\":\"Doe\",\"UserProfile_GUID\":\"\",\"AccountName\":\"Domain\\Doe\",\"FirstName\":\"John\",\"LastName\":\"Doe\",\"PreferredName\":\"John Doe\",\"WorkPhone\":\"2176\",\"Department\":\"PHI\",\"Title\":\"Director\",\"SPS-JobTitle\":\"Director\",\"Manager\":\"Domain\\jdoe\",\"PersonalSpace\":\"/my/personal/Doe/\",\"PictureURL\":\"Doe_MThumb.jpg\",\"UserName\":\"Doe\",\"SPS-ClaimProviderID\":\"Windows\",\"SPS-ClaimProviderType\":\"Windows\",\"SPS-SavedAccountName\":\"Domain\\Doe\",\"SPS-DistinguishedName\":\"CN=John Doe,CN=Users,DC=domain,DC=local\",\"SPS-LastKeywordAdded\":\"1/2/2014 12:00:00 AM\",\"WorkEmail\":\"Doe@domain.com\",\"CellPhone\":\"5555555555\",\"Office\":\"C28\",\"SPS-TimeZone\":\"WF_Service.UPS.SPTimeZone\",\"personalEmail\":\"Doe.John@gmail.com\",\"SPS-EmailOptin\":\"0\",\"empID\":\"224\",\"team\":\"A\",\"p_id\":\"224\",\"username\":\"Doe\",\"last_name\":\"Doe\",\"first_name\":\"John\",\"birthday\":\"1/2/1985 12:00:00 AM\",\"anniv\":\"6/4/2007 12:00:00 AM\",\"imageurl\":\"A0BB.jpg\",\"Position\":\"Director\",\"dept_started_in\":\"IT\",\"birthplace\":\"NC\",\"marital_status\":\"Married\",\"sports_team\":\"Sporty Sports\",\"last_school\":\"None\",\"hobbies\":\"\",\"pets\":\"\",\"prior_employer\":\"None\",\"resides_in\":\"USA\",\"quote\":\"“Where you live should not determine whether you live, or whether you die.”  -- Bono\",\"fav_color\":\"009933\"

【问题讨论】:

【参考方案1】:

你能试试下面链接中的方法吗

http://procbits.com/2011/04/21/quick-json-serializationdeserialization-in-c

【讨论】:

我尝试使用 Dictionary 得到同样的错误。

以上是关于jQuery/ASMX:在 C# 中反序列化 JSON 时出错的主要内容,如果未能解决你的问题,请参考以下文章

在 C# 中反序列化 JSON 数组

如何在 C# 中反序列化多个 JSON 对象?

在 C# 中序列化为 JSON 并在 TS 中反序列化

在 C# 中反序列化 XML [重复]

C# 在单个对象中反序列化两个 Jarray 对象

如何在 C# 中反序列化具有前缀的 XML