2015-04-07 13 views
7

Questo è un servizio WCF che riceve richieste dai dispositivi Android. La stessa richiesta funziona con i dispositivi Lollipop, non con i dispositivi jellybean, perché jellybean organizza il JSON in modo diverso durante la creazione.Errore di deserializzazione Newtonson JSON.net in cui i campi in ordine di modifica JSON

L'eccezione:

imprevisto segno quando deserializzazione oggetto: String. Percorso 'SearchFilters.config $ tipo.', La linea 1, la posizione 212.

non funzionante JSON:

{ 
    "DeviceType": 2, 
    "SearchFilters": { 
     "config": { 
      "$values": [ 
       { 
        "Collection": { 
         "DeviceType": 2 
        }, 
        "Category": "" 
       } 
      ], 
      "$type": "System.Collections.Generic.List`1[[Yoosh.SharedClasses.YooshConfig, YooshSharedClassesDll]], mscorlib" 
     } 
    }, 
    "RequestingUserId": "66666666-6666-6666-6666-666666666666", 
    "APIKey": "xxx" 
} 

lavoro JSON:

{ 
    "APIKey": "xxx", 
    "DeviceType": 2, 
    "RequestingUserId": "66666666-6666-6666-6666-666666666666", 
    "SearchFilters": { 
     "config": { 
      "$type": "System.Collections.Generic.List`1[[Yoosh.SharedClasses.YooshConfig, YooshSharedClassesDll]], mscorlib", 
      "$values": [ 
       { 
        "Category": "", 
        "Collection": { 
         "DeviceType": 2 
        } 
       } 
      ] 
     } 
    } 
} 

Alcuni campi sono in un diverso ordine .. Questa è l'unica differenza.

La classe C#:

public class QueryParameters 
{ 
    BaseParameters m_baseParameters; 
    Guid m_gRequestingUserId; 
    Dictionary<string, object> m_SearchFilters; 

    [DataMember] 
    public string APIKey 
    { 
     get { return m_baseParameters.APIKey; } 
     set { m_baseParameters.APIKey = value; } 
    } 

    [DataMember] 
    public BaseParameters.YooshDeviceType DeviceType 
    { 
     get { return m_baseParameters.DeviceType; } 
     set { m_baseParameters.DeviceType = value; } 
    } 

    [DataMember] 
    public string DeviceId 
    { 
     get { return m_baseParameters.DeviceId; } 
     set { m_baseParameters.DeviceId = value; } 
    } 

    [DataMember] 
    public Guid RequestingUserId 
    { 
     get { return m_gRequestingUserId; } 
     set { m_gRequestingUserId = value; } 
    } 

    [DataMember] 
    public Dictionary<string, object> SearchFilters 
    { 
     get { return m_SearchFilters; } 
     set { m_SearchFilters = value; } 
    } 
} 

versione Json.net: 6.0.8

+0

$ type è speciale. Non penso che tu possa riordinarlo. È ciò che Newtonsoft Json usa per determinare quale oggetto C# creare quando deserializza. Dato che hai un dizionario , probabilmente ha bisogno di $ type per capire quale tipo di oggetto creare. –

+0

Possibile duplicato di http://stackoverflow.com/questions/15570510 –

+2

'66666666-6666-6666-6666-666666666666' - l'ID della Bestia – arootbeer

risposta

11

Set JsonSerializerSettings.MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead.

Secondo il documentation:

Questo esempio deserializza JSON con MetadataPropertyHandling impostato ReadAhead modo che le proprietà dei metadati non hanno bisogno di essere all'inizio di un oggetto.

string json = @"{ 
    'Name': 'James', 
    'Password': 'Password1', 
    '$type': 'MyNamespace.User, MyAssembly' 
}"; 

object o = JsonConvert.DeserializeObject(json, new JsonSerializerSettings 
{ 
    TypeNameHandling = TypeNameHandling.All, 
    // $type no longer needs to be first 
    MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead 
}); 

Si noti che questa impostazione impact performance.

+1

Grazie, lo proverò dopo il fine settimana ma sono abbastanza sicuro questo funzionerà:] Si aggiornerà se non. –

Problemi correlati