我試圖下載一個JSON stringand將其分配給TextArea
:如何字符串解碼爲JSON
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;
}
}
類的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
?
你的類是完全錯誤的。它需要匹配JSON。 – SLaks
請不要將您的方法,變量,類或控件命名爲Polish ... – MarcinJuraszek
我建議您儘可能使用最簡單的方法,使用Json.net(一個免費的庫)。我想向你建議這個[nice site](http://json2csharp.com),它可以從Json字符串創建輸出c#類,因爲如果你決定不使用Json.net,它可能會非常有用。 –