[英]How to decode string as JSON
I'm trying to download a JSON stringand assigning it to a TextArea
: 我正在尝试下载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': '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; }
}
And grid: 和网格:
<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>
What's wrong? 怎么了? Why is root1
not assigned to textblock
? 为什么root1
没有分配给textblock
?
You can use below code as reference. 您可以使用以下代码作为参考。 Suppose you need to display the username in your textblock using facebook graph API 假设您需要使用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;
}
}
}
and json parser class (RootObject.cs) file should be like this, you can also use json2csharp.com for creating further json parser classes. 和json解析器类(RootObject.cs)文件应该是这样的,你也可以使用json2csharp.com来创建更多的json解析器类。 All parsed data is been stored in root1 you can further create reference to same. 所有已解析的数据都存储在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; }
}
In your xmal you have 在你的xmal你有
<TextBlock Name="usernamebox" Text="{Binding username}"></TextBlock>
now to assign json object value to textblock you can 现在可以将json对象值分配给textblock
this.usernamebox.DataContext = root1.username;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.