简体   繁体   中英

How to Display data in textblock after JSON Parsing

I am trying to parse some json data in my Windows application. I had written some codes for the same. The code is error free but no data appears in my textblock. Here is my 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" />

and this my class file

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;
        }

    }

This is the RootObject class file

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; }
}

When i added a watcher at data context it tells me the data is been fetched. But the data is not displayed in above given textblocks. please tell me the area i am doing things wrong or the correct method to display the data.

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

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

The json that you are trying to parse contains an array of RootObject. Hence you should be using a ListBox in XAML, like this:

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

During parsing you should use an array type, and set the list's ItemsSource property to it, like this:

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

Hope this helps. Mark as answer if it works out for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM