0

在这个 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; }
     }

 }
4

1 回答 1

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"]);
}

在此处输入图像描述

于 2012-12-01T22:38:29.607 回答