2013-02-13 12 views
5

Sto incontrando uno strano errore quando provo a deserializzare la seguente semplice stringa JSON:errore quando si cerca di deserializzare stringa JSON con json.Net

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

L'errore che sto ottenendo dalla DLL json.net è il seguente:

Invalid property identifier character: &. Path '', line 1, position 2. 

la traccia stack completo è il seguente:

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) 

Questo è il metodo che Sto usando per deserialising una stringa 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; 
} 

Sto avendo questo problema solo nell'ambiente SVS della nostra applicazione. La cosa strana è che lo stesso codice e gli stessi dati funzionano correttamente nei nostri ambienti di sviluppo e test.

Qualche idea sul perché json.net stia segnalando un problema con il carattere "&"? Non sembra avere molto senso ...

risposta

3

Sono riuscito a risolvere il problema da solo.

Si scopre che la stringa che stavo inviando al metodo "JSONDeserialize" era codificata in HTML.

La stringa JSON, invece di essere le seguenti:

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

era in realtà la seguente:

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

Al fine di risolvere il problema ho aggiornato il metodo 'JSONDeserialize' come segue:

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; 
} 
Problemi correlati