如何在JSON解析后显示文本块中的数据

时间:2013-11-19 11:37:44

标签: c# json windows-phone-7 windows-phone-8

我正在尝试解析Windows应用程序中的一些json数据。我写了一些相同的代码。代码没有错误,但我的文本块中没有数据。这是我的XAML

<TextBlock Name="acc1" Margin="180, 60, 0, 0"   Text="{Binding Accnumber1}" Foreground="White"  VerticalAlignment="Top" HorizontalAlignment="Left" FontFamily="Segoe UI" FontSize="22" />
                                <TextBlock Name="bal1" Margin="180, 90,0, 0"    Text="{Binding Availablebalance}" Foreground="White"  VerticalAlignment="Top" HorizontalAlignment="Left" FontFamily="Segoe UI" FontSize="22" />
                                <TextBlock Name="acc2" Margin="180, 140, 0, 0"  Text="{Binding Accnumber2}" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Left" FontFamily="Segoe UI" FontSize="22" />
                                <TextBlock Name="bal2" Margin="180, 170, 0, 0"  Text="{Binding Availablebalance}" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Left" FontFamily="Segoe UI" FontSize="22" />

和我的班级文件

public MainPage()
    {
        InitializeComponent();
        CheckForAnimation();
        BackKeyPress += OnBackKeyPressed;

        WebClient webClient = new WebClient();
        webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
        ProgressBarRequest.Visibility = System.Windows.Visibility.Visible;
        webClient.DownloadStringAsync(new Uri("http://mobimybank.appspot.com/loginresponse.json"));
    }



    void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        try
        {
            if (!string.IsNullOrEmpty(e.Result))
            {
                var root1 = JsonConvert.DeserializeObject<RootObject>(e.Result);
                  this.DataContext = root1;


            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            ProgressBarRequest.Visibility = System.Windows.Visibility.Collapsed;
        }

    }

这是RootObject类文件

public class Mybank
{
    public string status { get; set; }
}

public class Account
{
    public string Accnumber1 { get; set; }
    public string Availablebalance { get; set; }
    public string Accnumber2 { get; set; }
}

public class Accounts
{
    public List<Account> Account { get; set; }
}

public class Loginresponse
{
    public Mybank { get; set; }
    public Accounts { get; set; }
}

public class RootObject
{
    public Loginresponse loginresponse { get; set; }
}

当我在数据上下文中添加一个观察者时,它告诉我数据已被提取。但是数据不会显示在上面给出的文本块中。 请告诉我我做错的区域或显示数据的正确方法。

2 个答案:

答案 0 :(得分:2)

我得到了相同的解决方案,对于单个文本块声明将是这样的

this.txt1.DataContext= root1.loginresponse.Accounts.Account;

答案 1 :(得分:0)

您尝试解析的json包含一个RootObject数组。因此,您应该在XAML中使用ListBox,如下所示:

        <ListBox x:Name="list">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Brand}" />
                        <TextBlock Text="{Binding Type}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>    

在解析过程中,您应该使用数组类型,并将列表的ItemsSource属性设置为它,如下所示:

RootObject[] results = JsonConvert.DeserializeObject<RootObject[]>(json);
list.ItemsSource = results;

希望这会有所帮助。如果它适合您,请标记为答案。