C# 列表框不显示反序列化的JSON数据
我遇到这个问题好几天了,我解决不了。我的windows phone中的列表框没有显示任何内容。我甚至不确定json文件是否有效 注释的c#代码中包含一个json示例代码 这是WP8中C#的一部分C# 列表框不显示反序列化的JSON数据,c#,json,data-binding,windows-phone-8,listbox,C#,Json,Data Binding,Windows Phone 8,Listbox,我遇到这个问题好几天了,我解决不了。我的windows phone中的列表框没有显示任何内容。我甚至不确定json文件是否有效 注释的c#代码中包含一个json示例代码 这是WP8中C#的一部分 private void按钮\u单击\u 1(对象发送者,路由目标) { webserv.maintain_serverPortTypeClient tables=新的webserv.maintain_serverPortTypeClient(); tables.view_tableAsync(); t
private void按钮\u单击\u 1(对象发送者,路由目标)
{
webserv.maintain_serverPortTypeClient tables=新的webserv.maintain_serverPortTypeClient();
tables.view_tableAsync();
tables.view_tableCompleted+=新事件处理程序(tbviewcomplete);
}
public void tbviewcomplete(对象obj,webserv.view\u tableCompletedEventArgs e)
{
Show(例如Result.ToString());
//var table_json=e.Result.ToString();
//目前,该地区的非吸烟区、非吸烟区、非吸烟区、非吸烟区、区、区、区、区、区、区、区、区、区:室外、室外、室外、图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图图区域\“:\“吸烟\”,“区域\“:\“室内\”}];
//MyTables[]result=JsonConvert.DeserializeObject(table_json);
List gesult=JsonConvert.DeserializeObject(如Result);
tableview.ItemsSource=gesult;
}
公共类MyTables
{
公共字符串表_id{get;set;}
公共字符串表_size{get;set;}
公共字符串表_区域{get;set;}
公共字符串表_区域{get;set;}
}
这是XAML代码的一部分
<ListBox x:Name="tableview" ItemsSource="{Binding data}" HorizontalAlignment="Left" Height="474" VerticalAlignment="Top" Width="456">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel DataContext="{Binding}">
<TextBlock FontSize="{StaticResource PhoneFontSizeMedium}" Text="{Binding table_id}" />
<TextBlock Text="{Binding table_size}" />
<TextBlock Text="{Binding table_zone}" />
<TextBlock Text="{Binding table_area}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我不知道哪里出了问题,也没有显示出来。请给我提些建议。我对c#和Json非常陌生。因为我做了你想做的事,首先,您必须使用JSON字符串中使用的确切字段名,或者使用
System.Runtime.Serialization.DataContract
和System.Runtime.Serialization.DataMember
属性来修饰MyTable
类及其属性,以便使用任意属性名。所以MyTable
类在没有契约的情况下看起来是这样的:
public class MyTable
{
public string tableID { get; set; }
public int size { get; set; }
public string zone { get; set; }
public string area { get; set; }
}
或者像这样使用DataContract
:
using System.Runtime.Serialization;
...
[DataContract]
public class MyTable
{
[DataMember(Name = "tableID")]
public string table_id { get; set; }
[DataMember(Name = "size")]
public int table_size { get; set; }
[DataMember(Name = "zone")]
public string table_zone { get; set; }
[DataMember(Name = "area")]
public string table_area { get; set; }
}
注意,可以使用DataMember
属性的name
属性指定JSON字段名。这样,您就可以在类MyTable
中使用任何属性名
然后,使用此命令反序列化JSON字符串:
System.Runtime.Serialization.Json.DataContractJsonSerializer ser=new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List));
字符串表_json=e.Result.ToString();
列出结果;
使用(System.IO.MemoryStream ms=new System.IO.MemoryStream(Encoding.UTF8.GetBytes(table_json)))
{
结果=(列表)ser.ReadObject(毫秒);
}
请注意,必须将System.Runtime.Serialization.dll
添加到项目引用中。XAML
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Artist}"/>
<TextBlock Text="{Binding Duration}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
和C#代码:
公共类MyDataClass
{
公共字符串艺术家{get;set;}
公共字符串持续时间{get;set;}
公共MyDataClass(字符串艺术家,整数持续时间)
{
TimeSpan=TimeSpan.FromSeconds(持续时间);
艺术家=艺术家;
持续时间=span.ToString();
}
}
public void AudioGet()
{
var clientAudio=新的WebClient();
clientAudio.OpenReadCompleted+=clientAudio\u OpenReadCompleted;
字符串uri=”https://api.vk.com/method/audio.get?";
OpenReadAsync(新Uri(Uri));
}
私有void clientAudio_OpenReadCompleted(对象发送方,OpenReadCompletedEventArgs e)
{
List audioList=新列表();
var root=newdatacontractjsonserializer(typeof(RootObject));
RootObject RootObject=(RootObject)root.ReadObject(e.Result);
foreach(rootObject.response中的var myClass)
{
Add(新的MyDataClass(myClass.artist,myClass.duration));
}
MyListBox.ItemsSource=音频列表;
}
#区域JsonDataClass
公众课堂反应
{
公共字符串艺术家{get;set;}
公共整数持续时间{get;set;}
}
公共类根对象
{
公共列表响应{get;set;}
}
#端区
如果设置断点,则gesult
是否按预期包含大量结果?Tnx Brent供编辑。现在这个问题更有意义了@user3159789,问题不能包含标签,这就是为什么你的问题被编辑的原因。哦,我明白了,我不知道,因为我是stackoverflow的新手。显然这是我的第一篇文章。谢谢你的帮助!谢谢阿林先生!你教了我一些新东西,谢谢你的帮助。我非常感激。
System.Runtime.Serialization.Json.DataContractJsonSerializer ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<MyTable>));
string table_json = e.Result.ToString();
List<MyTable> result;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(table_json)))
{
result = (List<MyTable>)ser.ReadObject(ms);
}
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Artist}"/>
<TextBlock Text="{Binding Duration}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
public class MyDataClass
{
public string Artist { get; set; }
public string Duration { get; set; }
public MyDataClass(string artist, int duration)
{
TimeSpan span = TimeSpan.FromSeconds(duration);
Artist = artist;
Duration = span.ToString();
}
}
public void AudioGet()
{
var clientAudio = new WebClient();
clientAudio.OpenReadCompleted += clientAudio_OpenReadCompleted;
string uri = "https://api.vk.com/method/audio.get?";
clientAudio.OpenReadAsync(new Uri(uri));
}
private void clientAudio_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
List<MyDataClass> audioList = new List<MyDataClass>();
var root = new DataContractJsonSerializer(typeof(RootObject));
RootObject rootObject = (RootObject)root.ReadObject(e.Result);
foreach (var myClass in rootObject.response)
{
audioList.Add(new MyDataClass(myClass.artist, myClass.duration));
}
MyListBox.ItemsSource = audioList;
}
#region JsonDataClass
public class Response
{
public string artist { get; set; }
public int duration { get; set; }
}
public class RootObject
{
public List<Response> response { get; set; }
}
#endregion