0

I'm faced wiht an error, and I don't know how to correct it. I don't understand why it comes.

System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

The exception comes when I try to deserialize a JSON into objects that I created. (The second line code is throwing the exception)

List<Connection> connections = new List<Connection>();
var root = JsonConvert.DeserializeObject<RootObject2>(content);
connections = root.connections;

Here is the JSON that I want to use:

http://transport.opendata.ch/v1/connections?from=lausanne&to=fribourg

And here are my objects:

public class RootObject2
{
    public List<Connection> connections { get; set; }
}

 public class Connection
    {
        public Stop from { get; set; }
        public Stop to { get; set; }
        public string duration { get; set; }
        public int? transfers { get; set; }
        public Service service { get; set; }
        public List<string> products { get; set; }
        public int? capacity1st { get; set; }
        public int? capacity2nd { get; set; }
        public List<Section> sections { get; set; }
    }

  public class Stop
    {
        public Station station { get; set; }
        public DateTime arrival { get; set; }
        public int? arrivalTimestamp { get; set; }
        public string departure { get; set; }
        public int? departureTimestamp { get; set; }
        public int? delay { get; set; }
        public string platform { get; set; }
        public Prognosis prognosis { get; set; }
        public string realtimeAvailability {get; set;}
        public Station location { get; set; }
    }

   public class Station
    {
        public string id { get; set; }
        public string name { get; set; }
        public int? score { get; set; }
        public Coordinate coordinate { get; set; }
        public double? distance { get; set; }
    }

   public class Service
    {
        public string regular { get; set; }
        public string irregular { get; set; }
    }

   public class Section
    {
        public Journey journey { get; set; }
        public Walk walk { get; set; }
        public Stop departure { get; set; }
        public Stop arrival { get; set; }
    }


   public class Journey
        {
            public string name { get; set; }
            public string category { get; set; }
            public string subcategory { get; set; }
            public int? categoryCode { get; set; }
            public string number { get; set; }
            public string @operator { get; set; }
            public string to { get; set; }
            public List<Stop> passList { get; set; }
            public int? capacity1st { get; set; }
            public int? capacity2nd { get; set; }
        }


 public class Walk
    {
        public string duration { get; set; }
    }

public class Prognosis
{
    public string platform { get; set; }
    public string arrival { get; set; }
    public string departure { get; set; }
    public int? capacity1st { get; set; }
    public int? capacity2nd { get; set; }
}

public class Coordinate
{
    public string type { get; set; }
    public double x { get; set; }
    public double y { get; set; }
}

The deserialization is on a try and catch block, and nothing is catched. Also I am doing this on Android.

8
  • connections = JsonConvert.DeserializeObject<List<Connection>>(json);: Why are you not deserializing to the RootObject2, in this case? Commented Jun 23, 2017 at 15:37
  • @EBrown it's a mistake, I just edited my answer. I have this like you mentionned, I was just trying different possibilites. Commented Jun 23, 2017 at 15:41
  • It seems like you need to edit your answer again. Commented Jun 23, 2017 at 15:43
  • 1
    It is correct now Commented Jun 23, 2017 at 15:45
  • I imitated what you did, and I received this error: imgur.com/a/u1Zsa with var root = JsonConvert.DeserializeObject<RootObject2>(content); Commented Jun 23, 2017 at 16:04

1 Answer 1

2

Based on json data.

  • Arrival property in Stop class should be DateTime?.
Sign up to request clarification or add additional context in comments.

5 Comments

I vouch this fixes the problem! :)
It has indeed resolved my problem :O. Why is the "?" after the property for?
It's just syntax sugar for Nullable<DateTime>.
The ? says that it is of type Nullable<DateTime> rather than DateTime. Nullable types are used for value types which can't normally be null when you need null to be a valid option. See msdn.microsoft.com/en-us/library/b3h38hb0(v=vs.110).aspx for more info
Thank you all, I can now continue developing my project :D

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.