次の単純な 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, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, 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 が文字「&」に関する問題を報告している理由は何ですか? あまり意味がないように見えます...