Bonjour
J'essaye sans success de deserializer le résultat d'une requête sur une API rest
Ma compréhension a certainement dérapé qq part et je n'ai pas encore trouvé la bonne explication
Voici ma requête
1 2 3 4 5 6 7 8 9 10 11 12 13
| client.BaseAddress = new Uri(UR);
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// List data response.
HttpResponseMessage response = client.GetAsync(urlParameters).Result;
if (response.IsSuccessStatusCode)
{
var json = response.Content.ReadAsStringAsync().Result;
JsonConvert.DeserializeObject<C_Record16>(json); // Ne fonctionne pas ???
} |
La desérialisation me donne l'erreur suivante :

Envoyé par
JsonConvert.DeserializeObject<C_Record16>(json)
An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'TestGPS4NetRest.C_Record16' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
Pourtant ma chaine Json resultat me semble correcte
[{\"ALT\":\"17.70\",\"COG\":\"320.90\",\"DLOG\":\"1.00\",\"ENGINESTAT\":\"0\",\"ERROR\":0,\"FAVNETSTAT\":\"0\",\"GPRSSTAT\":\"1\",\"GSMLVL\":\"73.33\",\"GSMREGSTAT\":\"1\",\"GUS\":\"9\",\"IBAUTHSTAT\":\"0\",\"LAT\":\"50.901020\",\"LON\":\"3.407278\",\"NAVST\":\"1\",\"NST\":1,\"PANICBUTTONSTAT\":\"0\",\"PDOP\":\"2.4\",\"PRIVATESTAT\":\"0\",\"RELAYSTAT\":\"0\",\"ROAMINGSTAT\":\"1\",\"SBN\":\"08\",\"SEQ\":null,\"SOG\":\"0.00\",\"SWV\":\"22\",\"TIMESTAMP\":\"2016-11-13 07:00:09\",\"TRIGGER_SOURCE\":null,\"WARN\":0,\"gps_id\":\"2270\",\"id\":\"485\",\"msg_source\":\"2\"}]
Et j'ai aussi très patiemment construit ma classe destination en vérifiant plusieurs fois (ci dessous)
Quelle est mon erreur ?
Merci de votre aide
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| public class C_Record16
{
[JsonProperty("ALT")]
public string ALT { get; set; }
[JsonProperty("COG")]
public string COG { get; set; }
[JsonProperty("NAVST")]
public string NAVST { get; set; }
[JsonProperty("INPUTPWR_FAILURE")]
public string INPUTPWR_FAILURE { get; set; }
[JsonProperty("ENGINESTAT")]
public string ENGINESTAT { get; set; }
[JsonProperty("DLOG")]
public string DLOG { get; set; }
[JsonProperty("TRIGGER_SOURCE")]
public string TRIGGER_SOURCE { get; set; }
[JsonProperty("SWV")]
public string SWV { get; set; }
[JsonProperty("LAT")]
public string LAT { get; set; }
[JsonProperty("ROAMINGSTAT")]
public string ROAMINGSTAT { get; set; }
[JsonProperty("SEQ")]
public string SEQ { get; set; }
[JsonProperty("msg_source")]
public string msg_source { get; set; }
[JsonProperty("RELAYSTAT")]
public string RELAYSTAT { get; set; }
[JsonProperty("rectyp")]
public string rectyp { get; set; }
[JsonProperty("gps_id")]
public string gps_id { get; set; }
[JsonProperty("GUS")]
public string GUS { get; set; }
[JsonProperty("ACCSTAT")]
public string ACCSTAT { get; set; }
[JsonProperty("FAVNETSTAT")]
public string FAVNETSTAT { get; set; }
[JsonProperty("ACCUVOLTAGE_UNDERTRESHOLD")]
public string ACCUVOLTAGE_UNDERTRESHOLD { get; set; }
[JsonProperty("NST")]
public string NST { get; set; }
[JsonProperty("INPUTPWR_OVERVOLTAGE")]
public string INPUTPWR_OVERVOLTAGE { get; set; }
[JsonProperty("SOG")]
public string SOG { get; set; }
[JsonProperty("id")]
public string id { get; set; }
[JsonProperty("ERROR")]
public string ERROR { get; set; }
[JsonProperty("TIMESTAMP")]
public string TIMESTAMP { get; set; }
[JsonProperty("LON")]
public string LON { get; set; }
[JsonProperty("PANICBUTTONSTAT")]
public string PANICBUTTONSTAT { get; set; }
[JsonProperty("GSMREGSTAT")]
public string GSMREGSTAT { get; set; }
[JsonProperty("GSMLVL")]
public string GSMLVL { get; set; }
[JsonProperty("WARN")]
public string WARN { get; set; }
[JsonProperty("GPRSSTAT")]
public string GPRSSTAT { get; set; }
[JsonProperty("SBN")]
public string SBN { get; set; }
} |
Partager