c# – 尝试使用json.Net反序列化json字符串时出错
发布时间:2020-12-15 05:37:57 所属栏目:百科 来源:网络整理
导读:当我尝试反序列化以下简单的json字符串时遇到一个奇怪的错误: { "items": [ "IZ01","MTSQ","VM16","CR05" ]} 我从json.net dll得到的错误如下: Invalid property identifier character: . Path '',line 1,position 2. 完整堆栈跟踪如下: at Newtonsoft.Js
当我尝试反序列化以下简单的json字符串时遇到一个奇怪的错误:
{ "items": [ "IZ01","MTSQ","VM16","CR05" ]} 我从json.net dll得到的错误如下: Invalid property identifier character: &. Path '',line 1,position 2. 完整堆栈跟踪如下: at Newtonsoft.Json.JsonTextReader.ParseProperty() at Newtonsoft.Json.JsonTextReader.ParSEObject() at Newtonsoft.Json.JsonTextReader.ReadInternal() at Newtonsoft.Json.JsonTextReader.Read() at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CheckedRead(JsonReader reader) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader,Type objectType,JsonContract contract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerMember,Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader,Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader,Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader,Type objectType) at Metastorm.Runtime.Models.SIMSCommonLib.UtilitiesLib.JSONDeserialize(String jsonText,Type valueType) at Metastorm.Runtime.Models.JobProject.ExternalExtendedHelper.GetMaintenanceCodesForVehicle(String LPEntityCode,String UMC,String VIN,String EquipmentList) 这是我用于反序列化json字符串的方法: public static object JSONDeserialize(string jsonText,Type valueType) { Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer(); json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace; json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore; json.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; StringReader sr = new StringReader(jsonText); Newtonsoft.Json.JsonTextReader reader = new JsonTextReader(sr); object result = json.Deserialize(reader,valueType); reader.Close(); return result; } 我只在我们的应用程序的UAT环境中遇到此问题.奇怪的是,相同的代码和相同的数据在我们的开发和测试环境中正常工作. 知道为什么json.net报告字符“&”的问题? 解决方法
我设法自己解决了这个问题.
事实证明,我发送给方法’JSONDeserialize’的字符串是HTML编码的. JSON字符串,而不是以下内容: { "items": [ "IZ01","CR05" ]} 实际上是以下内容: { "items": [ "IZ01","MTSQ","VM16","CR05" ]} 为了解决这个问题,我更新了’JSONDeserialize’方法,如下所示: public static object JSONDeserialize(string jsonText,Type valueType) { // HTML-decode the string,in case it has been HTML encoded jsonText = System.Web.HttpUtility.HtmlDecode(jsonText); Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer(); json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace; json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore; json.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; StringReader sr = new StringReader(jsonText); Newtonsoft.Json.JsonTextReader reader = new JsonTextReader(sr); object result = json.Deserialize(reader,valueType); reader.Close(); return result; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |