【问题标题】:How to Display data in textblock after JSON ParsingJSON解析后如何在文本块中显示数据
【发布时间】:2013-11-19 11:37:43
【问题描述】:

我正在尝试在我的 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; }
}

当我在数据上下文中添加观察者时,它告诉我数据已被获取。但是数据没有显示在上面给定的文本块中。 请告诉我我做错的地方或显示数据的正确方法。

【问题讨论】:

  • 我会使用 DependencyProperties: Text ="{Binding RelativeSource={RelativeSource AncestorType={x:Type ClassReferingTo}, AncestorLevel=1}, Path=Brand_Name}" ..... 你的代码在后面:创建一个依赖对象(Brand_Name)来引用。你应该处理 depProps ...谷歌它:)
  • 您已将 TextBloxks 绑定到属性 Brand、Type。但是,当您更改 ObservableCollection 时,会使用属性 Results 引发事件。
  • 首先感谢您的回复,请您再简单介绍一下,以便我能准确理解我的错误。
  • 你在结果中得到了什么?你拿到名单了吗?如果是的话,你在哪里分配文本块上的内容?
  • 文本块字段为空。我在我的文本块中分配内容 - txt 7/8/9/10

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您尝试解析的 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;
    

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

    【讨论】:

    • @DeepeshDoshi JSON 解析工作正常。我认为现在的问题在于数据绑定。由于您获得的是 RootObject 数组,因此您应该使用 ListBox。我已经相应地修改了答案。
    • 错误:列表 - list.itemsource = 结果;非静态字段、方法或属性需要对象引用。这个错误是什么意思??
    • @DeepeshDoshi 它只要求您的结果变量应该是静态的。将其声明为静态外部函数定义,如static RootObject[] results;
    猜你喜欢
    • 2012-02-17
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    相关资源
    最近更新 更多