使用json.Net反序列化JSON字符串时出错。

5

在尝试反序列化以下简单的JSON字符串时,我遇到了一个奇怪的错误:

{ "items": [ "IZ01", "MTSQ", "VM16", "CR05" ]}

我得到的json.net dll错误如下:

从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报告字符"&"有问题吗?这似乎没有太多意义...

1个回答

4

我成功地解决了这个问题。

原来我发送到 'JSONDeserialize' 方法的字符串是HTML编码的。

JSON字符串不是下面这样的:

{ "items": [ "IZ01", "MTSQ", "VM16", "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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接