Skip to main content
added 2098 characters in body
Source Link
Joffrey Kern
  • 6.5k
  • 3
  • 27
  • 25

EDIT : With your pastebin classes, I have rebuild them and add a Callback on OnDeserialized :

class JSONResponse { public Line ROOT; } class Line { [JsonProperty("Time")] public Timestamp Time { get; set; } [JsonProperty("S")] public List<Station> S { get; set; } } class Timestamp { [JsonProperty("@TimeStamp")] public string @TimeStamp { get; set; } } class Station { [JsonProperty("@Code")] public string @Code { get; set; } [JsonProperty("@N")] public string @N { get; set; } [JsonProperty("P")] public List<Platform> P { get; set; } } class Platform { [JsonProperty("@N")] public string @N { get; set; } [JsonProperty("@Code")] public string @Code { get; set; } [JsonProperty("T")] public JToken T { get; set; } [OnDeserialized] internal void OnDeserializedMethod(StreamingContext context) { if (this.T != null) { if (this.T is JArray) { this.Trains = JsonConvert.DeserializeObject<List<Train>>(this.T.ToString()); } else { Train t = JsonConvert.DeserializeObject<Train>(this.T.ToString()); this.Trains = new List<Train>() { t }; } } } public List<Train> Trains; } class Train { [JsonProperty("@S")] public string @S { get; set; } [JsonProperty("@T")] public string @T { get; set; } [JsonProperty("@D")] public string @D { get; set; } [JsonProperty("@C")] public string @C { get; set; } [JsonProperty("@L")] public string @L { get; set; } [JsonProperty("@DE")] public string @DE { get; set; } } 

You will got your trains, on the Platform.Trains property

EDIT : With your pastebin classes, I have rebuild them and add a Callback on OnDeserialized :

class JSONResponse { public Line ROOT; } class Line { [JsonProperty("Time")] public Timestamp Time { get; set; } [JsonProperty("S")] public List<Station> S { get; set; } } class Timestamp { [JsonProperty("@TimeStamp")] public string @TimeStamp { get; set; } } class Station { [JsonProperty("@Code")] public string @Code { get; set; } [JsonProperty("@N")] public string @N { get; set; } [JsonProperty("P")] public List<Platform> P { get; set; } } class Platform { [JsonProperty("@N")] public string @N { get; set; } [JsonProperty("@Code")] public string @Code { get; set; } [JsonProperty("T")] public JToken T { get; set; } [OnDeserialized] internal void OnDeserializedMethod(StreamingContext context) { if (this.T != null) { if (this.T is JArray) { this.Trains = JsonConvert.DeserializeObject<List<Train>>(this.T.ToString()); } else { Train t = JsonConvert.DeserializeObject<Train>(this.T.ToString()); this.Trains = new List<Train>() { t }; } } } public List<Train> Trains; } class Train { [JsonProperty("@S")] public string @S { get; set; } [JsonProperty("@T")] public string @T { get; set; } [JsonProperty("@D")] public string @D { get; set; } [JsonProperty("@C")] public string @C { get; set; } [JsonProperty("@L")] public string @L { get; set; } [JsonProperty("@DE")] public string @DE { get; set; } } 

You will got your trains, on the Platform.Trains property

Source Link
Joffrey Kern
  • 6.5k
  • 3
  • 27
  • 25

You can check the type of you first property like this :

JObject obj = JObject.Parse(json); if (obj["T"] is JArray) { // hit on second case } else { // hit on first case } 

After that's, you can deserialize you object on List<T> or T.

Hope it's help !