Reputation: 2285
Using Json.Net in this Windows Phone application, I seem to be getting an exception from the JSON deserializer, which is the direct result of "events" returning as null. Here is what is important of the app's MainPage.xaml:
<ListBox x:Name="event_list" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding name}" TextWrapping="Wrap" />
<TextBlock Text="{Binding description}" Style="{StaticResource PhoneTextSubtleStyle}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
Here is the app's MainPage.xaml.cs:
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Xml;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace WPDevNorthTX
{
public partial class MainPage : PhoneApplicationPage
{
#region Constructor
// Constructor
public MainPage()
{
InitializeComponent();
}
#endregion
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
return;
}
List<Result> events = JsonConvert.DeserializeObject<List<Result>>(e.Result);
this.event_list.ItemsSource = events;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string requestUrl = @"https://api.meetup.com/2/events?key=766be5c39495111785c7947714359&sign=true&group_urlname=Windows-Phone-App-Development-Group-DFW-Dallas-Texas";
WebClient webClient = new WebClient();
webClient.DownloadStringAsync(new Uri(winPhoneGeekTweetsUrl));
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
}
}
public class Result
{
public string visibility { get; set; }
public string status { get; set; }
public int maybe_rsvp_count { get; set; }
public string id { get; set; }
public int utc_offset { get; set; }
public int duration { get; set; }
public object time { get; set; }
public int waitlist_count { get; set; }
public int yes_rsvp_count { get; set; }
public object created { get; set; }
public object updated { get; set; }
public string event_url { get; set; }
public string description { get; set; }
public int headcount { get; set; }
public string name { get; set; }
public Group group { get; set; }
public Venue venue { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
}
}
Upvotes: 0
Views: 1128
Reputation: 116168
Your json requires at least 4 classes and too many fields. Therefore I would go dynamic
way
dynamic dynObj = JsonConvert.DeserializeObject(e.Result);
Console.WriteLine(dynObj.meta.description);
foreach (var result in dynObj.results)
{
Console.WriteLine(result.description);
}
OR
var dynObj = (JObject)JsonConvert.DeserializeObject(e.Result);
Console.WriteLine(dynObj["meta"]["description"]);
foreach (var result in dynObj["results"])
{
Console.WriteLine(result["description"]);
}
Upvotes: 2