为什么Json.Net为事件的值返回null?

时间:2012-12-01 22:27:40

标签: c# json.net deserialization windows-phone

在这个Windows Phone应用程序中使用Json.Net,我似乎从JSON反序列化器中获得了一个异常,这是“事件”返回为null的直接结果。以下是应用程序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>

这是应用程序的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; }
     }

 }

1 个答案:

答案 0 :(得分:2)

您的json至少需要 4 类和太多字段。因此,我会dynamic方式

dynamic dynObj = JsonConvert.DeserializeObject(e.Result);

Console.WriteLine(dynObj.meta.description);

foreach (var result in dynObj.results)
{
    Console.WriteLine(result.description);
}

var dynObj = (JObject)JsonConvert.DeserializeObject(e.Result);

Console.WriteLine(dynObj["meta"]["description"]);

foreach (var result in dynObj["results"])
{
    Console.WriteLine(result["description"]);
}

enter image description here