简体   繁体   English

列表框未显示反序列化的JSON数据

[英]Listbox not displaying deserialized JSON data

I came across this problem for days , I couldnt solve it. 我连续几天遇到了这个问题,我无法解决。 The listbox in my windows phone is not displaying anything. Windows Phone中的列表框未显示任何内容。 I am not even sure whether the json deser works. 我什至不确定json deser是否有效。

A json sample code is inside within the c# code commented. json示例代码位于注释的C#代码内部。

This is the part of C# in WP8 这是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; }
}

And this is the part of the XAML code. 这是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>

I do not know where went wrong and it doesnt display. 我不知道哪里出了问题,它没有显示。 Please drop me down some advice. 请给我一些建议。 I am very new to c# and Json. 我是C#和Json的新手。

For doing what you want, first either you must use the exact field names used in JSON string or use System.Runtime.Serialization.DataContract and System.Runtime.Serialization.DataMember attributes to decorate MyTable class and its properties in order to use arbitrary property names. 为了执行所需的操作,首先必须使用JSON字符串中使用的确切字段名称,或者使用System.Runtime.Serialization.DataContractSystem.Runtime.Serialization.DataMember属性来装饰MyTable类及其属性,以便使用任意属性名称。 So MyTable class would look like this without contract: 所以MyTable类看起来像没有合同:

public class MyTable
{
    public string tableID { get; set; }
    public int size { get; set; }
    public string zone { get; set; }
    public string area { get; set; }
}

or like this with DataContract : 或像这样与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; }
}

Note that, JSON field name can be specified using Name property of DataMember attribute. 请注意,可以使用DataMember属性的Name属性指定JSON字段名称。 This way, you can use any property name in your class MyTable . 这样,您可以在类MyTable使用任何属性名称。

Then, use this to deserialize your JSON string: 然后,使用它来反序列化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);
}

Note that you must add System.Runtime.Serialization.dll to your project references. 请注意,必须将System.Runtime.Serialization.dll添加到项目引用中。

XAML 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>

and C# code: 和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

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

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