本文介绍了[UWP] [C#]如何将JSON内容读取到GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨 - 我很想读取json内容以列出视图。我有阅读xml内容的经验,但不是json,根据我的理解,它是不同的。所以这里是 我使用的 API Ticketmaster (目前我唯一感兴趣的是活动标题,地点,日期和单个图片)或者您可以使用 API资源管理器,以查看如何更好地组织json。目前我有为我想要的内容编写的类: class EventClass { public string Name {get;组; } public string Date {get;组; } public string Venue {get;组; } public string Image {get;组; } } 这就是我通常解析xml数据的方法,但我需要用json执行相同操作的等效代码: Uri requestUri = new Uri(" https://app.ticketmaster.com/discovery/v2/events.json?apikey = ZHQvjOwoHLazy25GC74HIGww694G869o& latlong =" + ApplicationData。 Current.LocalSettings.Values ["&的LatLong QUOT]。的ToString()); XmlDocument xmlDocument = await XmlDocument.LoadFromUriAsync(requestUri); XDocument xDoc = System.Xml.Linq.XDocument.Parse(xmlDocument.GetXml()); var data =来自xDoc.Descendants(" event")中的查询 select new EventClass { Name =(string)query.Element(" events")。元素(" name"), Date =(string)query.Element(" events")。Element(" dates")。Element(" start")。Element(" localDate") , Venue =(字符串)query.Element(" events")。元素(" embedded")。元素("场地")。元素(" 0")。元素(" name") ),图像=(字符串)query.Element(" events")。元素(" images")。元素(" 0")。元素(" url"),} ; List< EventClass> myList = data.ToList(); Event_List.ItemsSource = myList; 这是它所列出的界面: < GridView x:Name =" Event_List"的Horizo​​ntalAlignment = QUOT;拉伸"余量= QUOT; 0,48,0,0" Grid.RowSpan = QUOT; 2英寸VerticalAlignment = QUOT;拉伸"> < GridView.ItemTemplate> < DataTemplate> < Grid> < Image Source =" {Binding Image}" /> < Grid Margin =" 10"> < Grid.RowDefinitions> < RowDefinition /> < RowDefinition /> < RowDefinition /> < /Grid.RowDefinitions> < TextBlock Text =" {Binding Name}" Style =" {ThemeResource HeaderTextBlockStyle}" Grid.Row = QUOT; 0" /> < TextBlock Text =" {Binding Date}" Style =" {ThemeResource SubtitleTextBlockStyle}" Grid.Row = QUOT 1 QUOT; /> < TextBlock Text =" {Binding Venue}" Grid.Row = QUOT; 2英寸/> < / Grid> < / Grid> < / DataTemplate> < /GridView.ItemTemplate> < / GridView> 如果您有任何建议或我的答案,请告诉我。任何事情都会受到赞赏。谢谢! Zachary Bowling - ZAD Apps 解决方案 我正在使用  Newtonsoft.Json 现在。它很容易在UWP应用程序中使用。看看介绍:https://www.newtonsoft.com/json/help/html/Introduction.htm。它就像魔法一样。 Hi-I am new to the idea of reading json content to list views. I have experience in reading xml content, but not json and from what I understand, it is different. So here is theAPI I am using from Ticketmaster (currently the only thing I am interested in reading is the event title, venue, date, and a single image) or you can use theAPI explorer to view how the json is organized better. Currently I have the class written for the content I want:class EventClass { public string Name { get; set; } public string Date { get; set; } public string Venue { get; set; } public string Image { get; set; } }This is how I'd normally parse the data for xml, but I need the equivalent code for doing the same thing with json:Uri requestUri = new Uri("https://app.ticketmaster.com/discovery/v2/events.json?apikey=ZHQvjOwoHLazy25GC74HIGww694G869o&latlong=" + ApplicationData.Current.LocalSettings.Values["LatLong"].ToString()); XmlDocument xmlDocument = await XmlDocument.LoadFromUriAsync(requestUri); XDocument xDoc = System.Xml.Linq.XDocument.Parse(xmlDocument.GetXml()); var data = from query in xDoc.Descendants("event") select new EventClass { Name = (string)query.Element("events").Element("name"), Date = (string)query.Element("events").Element("dates").Element("start").Element("localDate"), Venue = (string)query.Element("events").Element("embedded").Element("venues").Element("0").Element("name"), Image = (string)query.Element("events").Element("images").Element("0").Element("url"), }; List<EventClass> myList = data.ToList(); Event_List.ItemsSource = myList;This is the interface to which it's listed:<GridView x:Name="Event_List" HorizontalAlignment="Stretch" Margin="0,48,0,0" Grid.RowSpan="2" VerticalAlignment="Stretch"> <GridView.ItemTemplate> <DataTemplate> <Grid> <Image Source="{Binding Image}"/> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <TextBlock Text="{Binding Name}" Style="{ThemeResource HeaderTextBlockStyle}" Grid.Row="0"/> <TextBlock Text="{Binding Date}" Style="{ThemeResource SubtitleTextBlockStyle}" Grid.Row="1"/> <TextBlock Text="{Binding Venue}" Grid.Row="2"/> </Grid> </Grid> </DataTemplate> </GridView.ItemTemplate> </GridView>If you have any suggestions or my answer, please let me know. Anything will be appreciated. Thanks!Zachary Bowling - ZAD Apps 解决方案 I'm using Newtonsoft.Json now. It's easy to use in UWP apps. Take a look at the introduction: https://www.newtonsoft.com/json/help/html/Introduction.htm. It works like magic. 这篇关于[UWP] [C#]如何将JSON内容读取到GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 02:12