Sto tentando di utilizzare un’API che usa la seguente struttura di esempio per il loro json restituito
[ { "customer":{ "first_name":"Test", "last_name":"Account", "email":"[email protected]", "organization":"", "reference":null, "id":3545134, "created_at":"2013-08-06T15:51:15-04:00", "updated_at":"2013-08-06T15:51:15-04:00", "address":"", "address_2":"", "city":"", "state":"", "zip":"", "country":"", "phone":"" } }, { "customer":{ "first_name":"Test", "last_name":"Account2", "email":"[email protected]", "organization":"", "reference":null, "id":3570462, "created_at":"2013-08-12T11:54:58-04:00", "updated_at":"2013-08-12T11:54:58-04:00", "address":"", "address_2":"", "city":"", "state":"", "zip":"", "country":"", "phone":"" } } ]
JSON.net funzionerebbe alla grande con qualcosa come la seguente struttura
{ "customer": { ["field1" : "value", etc...], ["field1" : "value", etc...], } }
Ma non riesco a capire come ottenerlo per essere felice con la struttura fornita.
L’utilizzo di JsonConvert.DeserializeObject (contenuto) predefinito restituisce il numero corretto di clienti ma tutti i dati sono nulli.
Fare qualcosa a CustomerList (sotto) produce un’eccezione “Can not deserialize the json array”
public class CustomerList { public List customer { get; set; } }
Pensieri?
È ansible creare un nuovo modello per deserializzare Json CustomerJson
:
public class CustomerJson { [JsonProperty("customer")] public Customer Customer { get; set; } } public class Customer { [JsonProperty("first_name")] public string Firstname { get; set; } [JsonProperty("last_name")] public string Lastname { get; set; } ... }
E puoi deserializzare facilmente il tuo json:
JsonConvert.DeserializeObject>(json);
Spero che sia d’aiuto !
Documentazione: serializzazione e deserializzazione JSON
Per coloro che non vogliono creare alcun modello, utilizzare il seguente codice:
var result = JsonConvert.DeserializeObject< List>>>(content);
Usando la risposta accettata devi accedere a ogni record usando Customers[i].customer
e hai bisogno di una class CustomerJson
più, che è un po ‘fastidiosa. Se non vuoi farlo, puoi usare quanto segue:
public class CustomerList { [JsonConverter(typeof(MyListConverter))] public List customer { get; set; } }
Nota che sto usando un List<>
, non un array. Ora crea la seguente class:
class MyListConverter : JsonConverter { public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var token = JToken.Load(reader); var list = Activator.CreateInstance(objectType) as System.Collections.IList; var itemType = objectType.GenericTypeArguments[0]; foreach (var child in token.Values()) { var childToken = child.Children().First(); var newObject = Activator.CreateInstance(itemType); serializer.Populate(childToken.CreateReader(), newObject); list.Add(newObject); } return list; } public override bool CanConvert(Type objectType) { return objectType.IsGenericType && (objectType.GetGenericTypeDefinition() == typeof(List<>)); } public override bool CanWrite => false; public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImplementedException(); }