数据绑定反序列化json数组
本文关键字:数组 json 反序列化 数据绑定 | 更新日期: 2023-09-27 18:18:17
我对c#、silverlight和整个数据绑定范例都比较陌生。我一直在做一个小的测试应用程序,通过他们的API使用Json.Net从reddit提取数据。无论如何,我可以很好地将数据放入应用程序,但现在我在将数据放入UI时遇到了麻烦。我尝试了几种不同的配置,但都无济于事。无论如何,代码在这里:
public partial class MainPage : PhoneApplicationPage
{
string json = "";
RootObject topic { get; set; }
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
textBlock1.Text = "Retrieving...";
string url = @"http://www.reddit.com/r/all.json";
HttpWebRequest hWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
hWebRequest.Method = "GET";
hWebRequest.BeginGetResponse(Response_Completed, hWebRequest);
}
public void Response_Completed(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
json = streamReader.ReadToEnd();
topic = JsonConvert.DeserializeObject<RootObject>(json);
}
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
this.DataContext = topic.data.children[0].data.title;
textBlock1.Text = "Done.";
});
}
这是我代码的主要部分。剩下的类在这里,它们用于reddit API提供的JSON的反序列化。
public class MediaEmbed
{
public string content { get; set; }
public int? width { get; set; }
public bool? scrolling { get; set; }
public int? height { get; set; }
}
public class Oembed
{
public string provider_url { get; set; }
public string description { get; set; }
public string title { get; set; }
public string url { get; set; }
public string author_name { get; set; }
public int height { get; set; }
public int width { get; set; }
public string html { get; set; }
public int thumbnail_width { get; set; }
public string version { get; set; }
public string provider_name { get; set; }
public string thumbnail_url { get; set; }
public string type { get; set; }
public int thumbnail_height { get; set; }
public string author_url { get; set; }
}
public class Media
{
public string type { get; set; }
public Oembed oembed { get; set; }
}
public class Data2
{
public string domain { get; set; }
public MediaEmbed media_embed { get; set; }
public object levenshtein { get; set; }
public string subreddit { get; set; }
public string selftext_html { get; set; }
public string selftext { get; set; }
public object likes { get; set; }
public bool saved { get; set; }
public string id { get; set; }
public bool clicked { get; set; }
public string title { get; set; }
public Media media { get; set; }
public int score { get; set; }
public bool over_18 { get; set; }
public bool hidden { get; set; }
public string thumbnail { get; set; }
public string subreddit_id { get; set; }
public string author_flair_css_class { get; set; }
public int downs { get; set; }
public bool is_self { get; set; }
public string permalink { get; set; }
public string name { get; set; }
public double created { get; set; }
public string url { get; set; }
public string author_flair_text { get; set; }
public string author { get; set; }
public double created_utc { get; set; }
public int num_comments { get; set; }
public int ups { get; set; }
}
public class Child
{
public string kind { get; set; }
public Data2 data { get; set; }
}
public class Data
{
public string modhash { get; set; }
public Child[] children { get; set; }
public string after { get; set; }
public object before { get; set; }
}
public class RootObject
{
public string kind { get; set; }
public Data data { get; set; }
}
}
假设XAML UI是这样的
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="TitleInfo" />
<TextBlock x:Name="AuthorInfo" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
标题包含在RootObject的实例topic中。抓取标题的方法是
topic.data.children[0].data.title;
然而,我几乎不知道如何将其绑定到这些文本框或列表框..我知道必须设置数据上下文,这些项目可以通过代码绑定,而不是xaml,但我不能想出任何优雅的方法来做到这一点。任何帮助吗?谢谢。
看起来你就快成功了。你的问题是,你试图设置整个页面的数据上下文是一个单一的标题从一个记录(this.DataContext = topic.data.children[0].data.title;
) -这可能不是你想要做的…
要将数据放入ListBox中,您有两个选项-您可以显式地设置ListBox的ItemsSource,如下所示
myListBox.ItemsSource = topic.data.children;
,然后更新XAML以在对象图中显示来自该点的数据…
<ListBox Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="TitleInfo" Text="{Binding data.title}" />
<TextBlock x:Name="AuthorInfo" Text="{Binding data.author}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
但是,如果您希望在页面的其他地方使用数据,则可能需要为整个页面设置DataContext。
DataContext = topic;
并将列表框上的XAML设置为稍微不同的内容…
<ListBox Name="MyListBox" ItemsSource="{Binding data.children}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="TitleInfo" Text="{Binding data.title}" />
<TextBlock x:Name="AuthorInfo" Text="{Binding data.author}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如果要处理databinding,最好实现MVVM模式。
同时,这里是你的例子的修复:
public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
{
string json = "";
private RootObject topic;
public event PropertyChangedEventHandler PropertyChanged;
public RootObject Topic
{
get
{
return this.topic;
}
set
{
this.topic = value;
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs("Topic"));
}
}
}
public MainPage()
{
InitializeComponent();
this.DataContext = this;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
...
}
public void Response_Completed(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
json = streamReader.ReadToEnd();
this.Topic = JsonConvert.DeserializeObject<RootObject>(json);
}
}
}
和更新后的xaml:
<ListBox Name="MyListBox" ItemsSource="{Binding Topic.data.children}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="TitleInfo" Text="{Binding data.title}" />
<TextBlock x:Name="AuthorInfo" Text="{Binding data.author}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>