c mvc json反斜杠問(wèn)題是在mvc開(kāi)發(fā)中json數(shù)據(jù)在傳遞過(guò)程中出現(xiàn)了缺陷,導(dǎo)致反斜杠的字符被加了一個(gè)反斜杠,從而影響數(shù)據(jù)的可用性。
例如: 數(shù)據(jù):{"name":"小明","age":18,"gender":"男","hobby":"籃球"} 傳輸后變成:{"name":"小明","age":18,"gender":"男","hobby":"籃球\\\"}
為了解決這個(gè)問(wèn)題,我們可以使用一個(gè)json序列化函數(shù)來(lái)進(jìn)行數(shù)據(jù)的反序列化。
例如: public static string SerializeObject(object obj) { string result = string.Empty; try { JavaScriptSerializer jss = new JavaScriptSerializer(); jss.MaxJsonLength = 2097152; result = jss.Serialize(obj); } catch (Exception ex) { result = ex.Message; } return result; } public static T DeserializeObject(string json) where T : class { try { JavaScriptSerializer jss = new JavaScriptSerializer(); jss.MaxJsonLength = 2097152; return jss.Deserialize (json); } catch (Exception ex) { return default(T); } }
將數(shù)據(jù)序列化后再進(jìn)行反序列化,即可成功解決c mvc json反斜杠的問(wèn)題。