-2

I need to consume an API service which returns the following. The object does not contain property names, there for I am not clear what the model or for each loop should look like. Please advise. Thanks

            {
              "0": [
                [
                  "01:08 PM - 01:28 PM",
                  "2017-07-19T13:08:24.000-07:00",
                  218
                ],
            [
              "01:23 PM - 01:43 PM",
              "2017-07-19T13:23:24.000-07:00",
              218
            ],
            [
              "01:38 PM - 01:58 PM",
              "2017-07-19T13:38:24.000-07:00",
              218
            ],
            [
              "01:53 PM - 02:13 PM",
              "2017-07-19T13:53:24.000-07:00",
              218
            ],
            [
              "02:08 PM - 02:28 PM",
              "2017-07-19T14:08:24.000-07:00",
              218
            ],
            [
              "02:23 PM - 02:43 PM",
              "2017-07-19T14:23:24.000-07:00",
              218
            ],
            [
              "12:53 PM - 01:13 PM",
              "2017-07-19T12:53:24.000-07:00",
              218
            ]
          ]
        }
2
  • 2
    It has a property name: "0" - and that contains an array of arrays Commented Jul 19, 2017 at 18:35
  • Why the negative sign on my question. I didn't author the API, I just have to consume it. Commented Jul 19, 2017 at 20:03

2 Answers 2

0

If you use an json to c# converter, it yields this:

public class RootObject
{
    public List<List<object>> __invalid_name__0 { get; set; }
}

Unfortunately, 0 is not an allowed proerty name in C# You can use newtonsoft JSON to map the property to a different name:

public class RootObject
{
   [JsonProperty("0")]
    public List<List<object>> zero { get; set; }
}

But that yields an extremely unwieldy construct

Probably this would be better:

public class RootObject
{
   [JsonProperty("0")]
    public List<InnerObj> zero { get; set; }
}

public class InnerObj
{
    public string Range { get; set; }
    public string Date { get; set; }
    public string Code { get; set; }
}

To deserialize this, see this question: JSON deserialization - Map array indices to properties with JSON.NET

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I did try json2sharp converter first and didn't help. I will give what you have a shot.
-1

Based on Christian's answer, this is what I am going to go with:

public class Times
{
    [JsonProperty("0")]
    public List<TimeObject> times { get; set; }
}

public class TimeObject
{
    public string Range { get; set; }
    public string Date { get; set; }
    public string Code { 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.