2013-11-27 115 views
0

我試圖下載一個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

+1

你的類是完全錯誤的。它需要匹配JSON。 – SLaks

+3

請不要將您的方法,變量,類或控件命名爲Polish ... – MarcinJuraszek

+0

我建議您儘可能使用最簡單的方法,使用Json.net(一個免費的庫)。我想向你建議這個[nice site](http://json2csharp.com),它可以從Json字符串創建輸出c#類,因爲如果你決定不使用Json.net,它可能會非常有用。 –

回答

2

您可以使用下面的代碼作爲參考。假設你需要使用Facebook的圖形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對象價值的文本塊,你可以

this.usernamebox.DataContext = root1.username; 
+0

好吧,我的上面的例子改變班後工作正常。現在我更改json源代碼和類(使用json2csharp.com),它不起作用。我有這個錯誤:Newtonsoft.Json.Json.DLL中發生類型'Newtonsoft.Json.JsonReaderException'的異常,並且未在託管/本地邊界之前處理 類型爲'Newtonsoft.Json.JsonReaderException'的第一次機會異常發生在Newtonsoft.Json.DLL 在Newtonsoft.Json.DLL中發生了類型爲「Newtonsoft.Json.JsonReaderException」的異常,並且未在託管/本機邊界之前處理 請問您能幫助我嗎? – breq

+0

catch返回此錯誤'錯誤! :解析值時遇到意外的字符:<。路徑'',第0行,位置0.'。我是c#中的新手,我不知道如何開始修復這個問題 – breq

+0

檢查以下內容:1)您綁定的文本塊應該與您的類中的json對象完全相同。 2)檢查您通過瀏覽器REST客戶端插件/插件中的URI獲取的響應,並在Visual Studio中打印輸出。它會給你一個簡單的想法如何解決這個問題。 – Dexto