我遇到这个问题好几天了,我无法解决。我的Windows手机中的列表框没有显示任何内容。我甚至不确定json deser是否有效。
json示例代码位于c#代码中。
这是WP8中C#的一部分
private void Button_Click_1(object sender, RoutedEventArgs e)
{
webserv.maintain_serverPortTypeClient tables = new webserv.maintain_serverPortTypeClient();
tables.view_tableAsync();
tables.view_tableCompleted += new EventHandler<webserv.view_tableCompletedEventArgs>(tbviewcomplete);
}
public void tbviewcomplete(object obj, webserv.view_tableCompletedEventArgs e)
{
MessageBox.Show(e.Result.ToString());
// var table_json = e.Result.ToString();
// var table_json = "[{\"tableID\":\"61\",\"size\":\"4\",\"zone\":\"Non-Smoking\",\"area\":\"Outdoor\"},{\"tableID\":\"62\",\"size\":\"4\",\"zone\":\"Non-Smoking\",\"area\":\"Outdoor\"},{\"tableID\":\"63\",\"size\":\"4\",\"zone\":\"Smoking\",\"area\":\"Indoor\"},{\"tableID\":\"64\",\"size\":\"30\",\"zone\":\"Smoking\",\"area\":\"Indoor\"}]";
// MyTables[] result = JsonConvert.DeserializeObject<MyTables[]>(table_json);
List<MyTables> gesult = JsonConvert.DeserializeObject<List<MyTables>>(e.Result);
tableview.ItemsSource = gesult;
}
public class MyTables
{
public string table_id { get; set; }
public string table_size { get; set; }
public string table_zone { get; set; }
public string table_area { 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的新手。
答案 0 :(得分:0)
为了做你想做的事情,首先要么必须使用 JSON 字符串中使用的确切字段名称,要么使用System.Runtime.Serialization.DataContract
和System.Runtime.Serialization.DataMember
属性来装饰MyTable
class及其属性,以便使用任意属性名称。所以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; }
}
请注意,可以使用Name
属性的DataMember
属性指定 JSON 字段名称。这样,您就可以在班级MyTable
中使用任何属性名称。
然后,使用它来反序列化 JSON 字符串:
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);
}
请注意,您必须将System.Runtime.Serialization.dll
添加到项目参考中。
答案 1 :(得分:0)
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#代码:
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