2013-11-27 80 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