0

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.

5
  • I would use DependencyProperties: Text ="{Binding RelativeSource={RelativeSource AncestorType={x:Type ClassReferingTo}, AncestorLevel=1}, Path=Brand_Name}" ..... your code behind: create a dependency object (Brand_Name) to refer to. You should deal with depProps ... google it :) Commented Nov 19, 2013 at 11:44
  • You have bound TextBloxks to properties Brand, Type. But when you change ObservableCollection the event is raised with property Results. Commented Nov 19, 2013 at 11:51
  • firstly thanks for responding, i would request you to please brief a little more so that i can exactly understand my mistake. Commented Nov 19, 2013 at 12:00
  • what are you getting in results ? are you getting the list? if yes the where are you assigning the contents on textblock? Commented Nov 19, 2013 at 14:15
  • The textblock fields are empty. I am assigning the content in my textblock - txt 7/8/9/10 Commented Nov 20, 2013 at 6:47

2 Answers 2

2

I got the solution for the same, the declaration will be like this for a single text block

this.txt1.DataContext= root1.loginresponse.Accounts.Account;
Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

@DeepeshDoshi The JSON parsing is working properly. I think now the problem is with data binding. Since you are getting an array of RootObject, you should use a ListBox. I have modified the answer accordingly.
Error: list - list.itemsource = results; An object reference is required for the non-static field, method, or property. what does this error means ??
@DeepeshDoshi It simply requires that your results variable should be static. Declare it as static outside function definition, like static RootObject[] results;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.