1

I am trying to parse a JSON Array with no success.I researched all possible solutions but none seems to work for my solution.

This is the JSON returning from server:

{"events":[{"url":"xxxxx","title":"xxxxx","date":"xxxx","time":"xxxx"},{"url":"xxxxx","title":"xxxxx","date":"xxxx","time":"xxxx"}]}

This is my RootObject class :

public class ChristmasEvent
{
    [JsonProperty("events")]
    public List<Event> Events { get; set; }
}

public class Event
{
    [JsonProperty("url")]
    public string Url { get; set; }

    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("date")]
    public string Date { get; set; }

    [JsonProperty("time")]
    public string Time { get; set; }
}

Web Service class

 private const string Url = "http://xxxx/events";
 private HttpClient _httpClient = new HttpClient();

 var apiResource = await _httpClient.GetStringAsync(Url);

 var tr = JsonConvert.DeserializeObject<List<ChristmasEvent>>(apiResource);

 ObservableCollection<ChristmasEvent> christmasEvents = new ObservableCollection<ChristmasEvent>(tr);

 EventListView.ItemsSource = christmasEvents;

XAML File

<ListView x:Name="EventListView">
      <ListView.ItemTemplate>
        <DataTemplate>
           <ViewCell>
              <StackLayout>
                 <Label Text="{Binding title}" />
                 <Label Text="{Binding date, StringFormat='{0:MMMM dd, yyyy}'}"/>
              </StackLayout>
          </ViewCell>
         </DataTemplate>
       </ListView.ItemTemplate>
     </ListView>

I am unsure why this is not working and seeking to find what i am doing wrong..

1
  • please, always provide error that you get along with the question. It's hard to tell what exactly goes wrong Commented Dec 14, 2018 at 19:29

1 Answer 1

1

1) You are trying to deserialize to a List of items, while you should deserialize as the item itself (as I can see from the JSON you provided):

var tr = JsonConvert.DeserializeObject<ChristmasEvent>(apiResource);

Then

var events = new ObservableCollection<Event>(tr.Events);
EventListView.ItemsSource = events;

2) You bind to lowercase properties. Bind to Title instead of title and Date instead of date. Or simply name your C# properties as lowercase. For example:

<Label Text="{Binding Title}" />

3) Your Date property is declared as a string. But in your view you are trying to format it (with StringFormat) as a date. Make it a DateTime property. You may also want to change your Time property type to TimeSpan.

public DateTime Date { get; set; }
Sign up to request clarification or add additional context in comments.

7 Comments

That doesnt seem to be the issue. It is crashing at this point : var tr = JsonConvert.DeserializeObject<List<ChristmasEvent>>(apiResource); with error message : Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List
With this var tr = JsonConvert.DeserializeObject<ChristmasEvent>(apiResource); how will i get the deserialize object as an Observable Collection to send to the view?
Simply. EventListView.ItemsSource = new ObservableCollection<Event>(tr.Events) and upvote my answer if you find it helpful
That throws the following error: Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable
Where does it throw this? Which line? Does it happen when you are running the app or when you try to compile?
|

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.