繁体   English   中英

如何在 C# 和 xaml 中使用 poco 类为 UWP 应用程序显示 json 值

[英]How to display json values using poco class in c# and xaml for UWP application

p_json:

{
"title":"title1",
"value1":"value1",
"objects":{
  "obj1":"obj1",
"obj2":"obj2"
},
"lists":[
{
"time":"11:00 Hrs",
"date":"jan"
},
{
"time":"12:00 hrs",
"date":"feb"
},
{
"time":"10:00",
"date":"mar"
}
]
}

波科 :::

namespace windows_app.Models
{
    public class EDetails
    {
        [JsonProperty("title")]
        public string Title { get; set; }

        [JsonProperty("value1")]
        public string Value1 { get; set; }

        [JsonProperty("objects")]
        public Object Objects { get; set; }

        [JsonProperty("lists")]
        public List<List> Lists { get; set; }

    }

    public class Object
    {
        [JsonProperty("obj1")]
        public string Obj1 { get; set; }

        [JsonProperty("obj2")]
        public string Obj2 { get; set; }

    }

    public class List
    {
        [JsonProperty("date")]
        public string Date { get; set; }

        [JsonProperty("time")]
        public string Time { get; set; }
    }

}

。CS::

string FilePath ="./Models/pdet.json";

            rootObject = JsonConvert.DeserializeObject<EDetails>(File.ReadAllText(FilePath));

            using (StreamReader file = File.OpenText(FilePath))
            {
                var json = file.ReadToEnd();
                Dictionary<string, object> result = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
                string tLists = result["lists"].ToString();
                List<List> objResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<List<List>>(tourLists);
                System.Diagnostics.Debug.WriteLine("objResponse::" + objResponse);
                tListItems.ItemsSource = objResponse;
                System.Diagnostics.Debug.WriteLine("tItems.ItemsSource " + tItems.ItemsSource);
            }

.xaml

 <ListView x:Name="tItems" Grid.ColumnSpan="5">
                                <Border  BorderThickness="0 1 0 1" BorderBrush="Gray"  >
                                    <StackPanel Orientation="Horizontal" Margin="5"  >
                                        <TextBlock Text="{Binding Date}"  Grid.Column="0"  Margin="10" FontSize="25" TextAlignment="Right"/>
                                        <TextBlock Text="{Binding Time}" Grid.Column="1" Margin="10" FontSize="25" TextAlignment="Right"/>

                                    </StackPanel>
                                </Border>
                            </ListView>

开/关:

一月 11:00 时

2 月 12:00 时

三月 10:00 时

我想要这样的输出。 但是,我没有得到这样的输出。 我得到如下结果

windows_app.Models.lists

如何解决这个问题呢。 有人帮我解决这个问题。 我试过了,但找不到解决方案。 我已经花了一天时间来解决这个问题。

  • 您面临的问题是您试图直接解析List类型的List ,它具有 2 个属性DateTime

  • 导致意外输出,例如windows_app.Models.lists

  • 创建了一个简单的 wpf 应用程序来重现问题,请在下面找到您可以调整以解决问题的代码段


.MainWindow.xaml.cs

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new DataObject();
        }
    }

    public class EDetails
    {

        [JsonProperty("title")]
        public string Title { get; set; }

        [JsonProperty("value1")]
        public string Value1 { get; set; }

        [JsonProperty("objects")]
        public Object Objects { get; set; }

        [JsonProperty("lists")]
        public List<List> Lists { get; set; }
    }

    public class Object
    {
        [JsonProperty("obj1")]
        public string Obj1 { get; set; }

        [JsonProperty("obj2")]
        public string Obj2 { get; set; }
    }

    public class List
    {
        [JsonProperty("date")]
        public string Date { get; set; }

        [JsonProperty("time")]
        public string Time { get; set; }
    }
    public class DataObject
    {

        public IList<string> NewDateTimeList { get; set; }

        public DataObject()
        {

            string FilePath = @"C:\Users\Win10EGL\source\repos\Test\testJson.json";

            var rootObject = JsonConvert.DeserializeObject<EDetails>(File.ReadAllText(FilePath));

            NewDateTimeList = new List<string>();
            foreach (var element in rootObject.Lists.ToList())
            {

                NewDateTimeList.Add(element.Date + " " + element.Time);
            }
        }
    }

.MainWindow.xaml

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListView HorizontalAlignment="Left" Height="100" Margin="345,260,0,0" VerticalAlignment="Top" Width="100" ItemsSource="{Binding NewDateTimeList}"/>

    </Grid>
</Window>

输出

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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