我正在尝试下载JSON字符串并将其分配给TextArea
:
private async void KliknijMnie_Click(object sender, RoutedEventArgs e)
{
try
{
ProgressBarRequest.Visibility = System.Windows.Visibility.Visible;
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("http://someurl.with?parameters=inURL");
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
var root1 = JsonConvert.DeserializeObject<uzytkownika>(responseBody);
this.DataContext = root1;
//fileName.Text = root1;
}
catch
{
string responseBody = "some errors";
}
finally
{
ProgressBarRequest.Visibility = System.Windows.Visibility.Collapsed;
}
}
class'uzytkownika':
public class Day1
{
public int @int { get; set; }
public string str { get; set; }
public string color { get; set; }
public object text { get; set; }
public string czas { get; set; }
public bool funkcje { get; set; }
}
public class Day2
{
public int @int { get; set; }
public string str { get; set; }
public string color { get; set; }
public object text { get; set; }
public string czas { get; set; }
public bool funkcje { get; set; }
}
public class Day3
{
public int @int { get; set; }
public string str { get; set; }
public string color { get; set; }
public object text { get; set; }
public string czas { get; set; }
public bool funkcje { get; set; }
}
(....)
public class Day30
{
public int @int { get; set; }
public string str { get; set; }
public string color { get; set; }
public object text { get; set; }
public string czas { get; set; }
public bool funkcje { get; set; }
}
public class Grafik
{
public Day1 day1 { get; set; }
public Day2 day2 { get; set; }
public Day3 day3 { get; set; }
(....)
public Day30 day30 { get; set; }
}
public class uzytkownika
{
public string imie { get; set; }
public string nazwisko { get; set; }
public bool status { get; set; }
public string msg { get; set; }
public string miesiacTXT { get; set; }
public string lastUpdate { get; set; }
public string updatedBy { get; set; }
public Grafik grafik { get; set; }
}
和网格:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Name="KliknijMnie" Margin="10,10,254,507" Click="KliknijMnie_Click">pobierz dane</Button>
<StackPanel Margin="0,143,0,10">
<StackPanel Orientation="Horizontal">
<TextBlock Width="200" Text="First name"></TextBlock>
<TextBlock Text="{Binding imie}" Width="230"></TextBlock>
</StackPanel>
</StackPanel>
<StackPanel Margin="0,143,0,10">
<StackPanel Orientation="Horizontal">
<TextBlock Width="200" Text="LastName"></TextBlock>
<TextBlock Text="{Binding nazwisko}" Width="230"></TextBlock>
</StackPanel>
</StackPanel>
</Grid>
怎么了?为什么root1
未分配给textblock
?
答案 0 :(得分:2)
您可以使用以下代码作为参考。假设您需要使用facebook graph API
在文本块中显示用户名private void KliknijMnie_Click(object sender, RoutedEventArgs e)
{
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
ProgressBarRequest.Visibility = System.Windows.Visibility.Visible;
webClient.DownloadStringAsync(new Uri("http://graph.facebook.com/stackoverflow"));
}
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
if (!string.IsNullOrEmpty(e.Result))
{
var root1 = JsonConvert.DeserializeObject<RootObject>(e.Result);
this.DataContext = root1;
}
}
}
和json解析器类(RootObject.cs)文件应该是这样的,你也可以使用json2csharp.com来创建更多的json解析器类。所有已解析的数据都存储在root1中,您可以进一步创建对它的引用。
public class Location
{
public string street { get; set; }
public string city { get; set; }
public string state { get; set; }
public string country { get; set; }
public string zip { get; set; }
}
public class Cover
{
public long cover_id { get; set; }
public string source { get; set; }
public int offset_y { get; set; }
public int offset_x { get; set; }
}
public class RootObject
{
public string about { get; set; }
public string category { get; set; }
public string founded { get; set; }
public bool is_published { get; set; }
public Location location { get; set; }
public string mission { get; set; }
public string phone { get; set; }
public int talking_about_count { get; set; }
public string username { get; set; }
public int were_here_count { get; set; }
public string id { get; set; }
public string name { get; set; }
public string link { get; set; }
public int likes { get; set; }
public Cover cover { get; set; }
}
在你的xmal中你有
<TextBlock Name="usernamebox" Text="{Binding username}"></TextBlock>
现在将json对象值分配给textblock,你可以
this.usernamebox.DataContext = root1.username;