0
我正在對C#ASMX網絡方法做AJAX POST,但無法獲取JSON數據反序列化。這應該工作,但是是給我下面的錯誤...jQuery/ASMX:在C#中反序列化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\":\"[email protected]\",\"CellPhone\":\"5555555555\",\"Office\":\"C28\",\"SPS-TimeZone\":\"WF_Service.UPS.SPTimeZone\",\"personalEmail\":\"[email protected]\",\"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\":\"[email protected]\",\"CellPhone\":\"5555555555\",\"Office\":\"C28\",\"SPS-TimeZone\":\"WF_Service.UPS.SPTimeZone\",\"personalEmail\":\"[email protected]\",\"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\"}}
我使用詞典<字符串,動態>試過,我得到同樣的錯誤。 – Fubak