2

I'm returning data from transportapi.com & loading it into an object, but the data within the json array isn't being included.

The json returned is:

{
  "atcocode": "490012745J",
  "smscode": "47889",
  "request_time": "2016-11-11T22:10:42+00:00",
  "departures": {
    "55": [
      {
        "mode": "bus",
        "line": "55",
        "line_name": "55",
        "direction": "Bakers Arms",
        "operator": "TFL",
        "date": null,
        "expected_departure_date": "2016-11-11",
        "aimed_departure_time": null,
        "expected_departure_time": "22:19",
        "best_departure_estimate": "22:19",
        "source": "Countdown instant"
      }
    ]
  }
}

Controller code:

var source = "http://transportapi.com/v3/uk/bus/stop/490012745J/live.json?api_key=[key]&app_id=[appid]";

Uri sourceUri = new Uri(source);
System.Net.Http.HttpClient sourceClient = new System.Net.Http.HttpClient();
System.Net.Http.HttpResponseMessage sourceResponse = await sourceClient.GetAsync(sourceUri);
var sourceArray = await sourceResponse.Content.ReadAsStringAsync();

var selections = JsonConvert.DeserializeObject<RootObject>(sourceArray);

Model class:

public class BusDepartures
{
    public string mode { get; set; }
    public string line { get; set; }
    public string line_name { get; set; }
    public string direction { get; set; }
    public string busoperator { get; set; }
    public object date { get; set; }
    public string expected_departure_date { get; set; }
    public object aimed_departure_time { get; set; }
    public string expected_departure_time { get; set; }
    public string best_departure_estimate { get; set; }
    public string source { get; set; }
}

public class Departures
{
    // First attempt, use BusDepartures object.
    //public List<BusDepartures> BusDepartures { get; set; }

    // Second attempt (as "55" is an array), use array, then convert to object later.
    public string[] routes { get; set; }
}

public class RootObject
{
    public string atcocode { get; set; }
    public string smscode { get; set; }
    public string request_time { get; set; }
    public Departures departures { get; set; }
}

Within the Departures class I did try and create a BusDepartures object to store the details of the departures, but I wondered whether, as it is an array, I should use the routes array instead? However when stepping through the code, the BusDepartures object (when it was uncommented) and the routes array were both null.

Any ideas? What am I missing?

Update:

Thanks to botond.botos for the answer. I amended my class to

public class RootObject
{
    public string atcocode { get; set; }
    public string smscode { get; set; }
    public string request_time { get; set; }
    public IDictionary<int, IEnumerable<BusDepartures>> departures { get; set; }
}

and it worked.

1
  • "departures": {"55": [{ ... }] cannot bind to a model - it would mean Departures could need a property named 55 which is invalid. If it was "departures": {"BusDepartures": [{ ... }] then it would bind to public List<BusDepartures> BusDepartures { get; set; } Commented Nov 11, 2016 at 23:40

2 Answers 2

2

You won't need the Departures class, and try to change the RootObject class the following way:

public class RootObject
{
    public string atcocode { get; set; }
    public string smscode { get; set; }
    public string request_time { get; set; }
    public IDictionary<int, IEnumerable<BusDepartures>> departures { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I'm pretty sure your trying to map the property name "55" to a non-existent c# property. Either modify the json (which i'm assuming you can't) or use a more generic property to handle the departures.

I'm thinking maybe something like a generic Dictionary(Of int, List(of Departure)) .... which is of course vb and I'm going to guess at new Dictionary<@int, List>() in c# ? :)

public class departures {

    public Dictionary<int, List<Departure>> BusDepartures {get; set; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.