Windows phone 7 将JSON数组反序列化到LongListSelector Windows Phone 8
如何反序列化此jsonx变量并将其放在windows phone上的longlistselector上 目前我只能用messagebox回显一个字符串。我需要longlistselector上的键值集 编辑:这是我的答案 这是我的密码Windows phone 7 将JSON数组反序列化到LongListSelector Windows Phone 8,windows-phone-7,windows-mobile,Windows Phone 7,Windows Mobile,如何反序列化此jsonx变量并将其放在windows phone上的longlistselector上 目前我只能用messagebox回显一个字符串。我需要longlistselector上的键值集 编辑:这是我的答案 这是我的密码 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Cont
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp5.Resources;
using Newtonsoft.Json;
namespace PhoneApp5
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
string json = @"{
'Email': 'james@example.com',
'Active': true,
'CreatedDate': '2013-01-20T00:00:00Z'
}";
string jsonx = @"{
'Table1': [
{
'id': 0,
'item': 'item 0'
},
{
'id': 1,
'item': 'item 1'
}
]
}";
Account account = JsonConvert.DeserializeObject<Account>(json);
// Console.WriteLine(account.Email);
// james@example.com
MessageBox.Show(account.Email);
list.ItemsSource = new BookList();
}
}
public class BookList : List<Account>
{
public BookList()
{
}
}
public class Account
{
public string Email { get; set; }
public bool Active { get; set; }
public DateTime CreatedDate { get; set; }
// public IList<string> Roles { get; set; }
}
///////////////////////////
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Navigation;
使用Microsoft.Phone.Controls;
使用Microsoft.Phone.Shell;
使用PhoneApp5.资源;
使用Newtonsoft.Json;
名称空间PhoneApp5
{
公共部分类主页:PhoneApplicationPage
{
//建造师
公共主页()
{
初始化组件();
字符串json=@”{
“电子邮件”:james@example.com',
“活动”:正确,
“CreatedDate”:“2013-01-20T00:00:00Z”
}";
字符串jsonx=@”{
“表1”:[
{
“id”:0,
“项”:“项0”
},
{
“id”:1,
“项目”:“项目1”
}
]
}";
Account=JsonConvert.DeserializeObject(json);
//Console.WriteLine(account.Email);
// james@example.com
MessageBox.Show(account.Email);
list.ItemsSource=newbooklist();
}
}
公共类图书目录:列表
{
公共书目()
{
}
}
公共类帐户
{
公共字符串电子邮件{get;set;}
公共bool活动{get;set;}
公共日期时间CreatedDate{get;set;}
//公共IList角色{get;set;}
}
///////////////////////////
}
还有我的XAML
<phone:PhoneApplicationPage
x:Class="PhoneApp5.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:LongListSelector Name="list">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding id}" />
<TextBlock Text=" ( " />
<TextBlock Text="{Binding item}" FontStyle="Italic" />
<TextBlock Text=" )" />
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
您需要一个类来表示json:
public class MyClass
{
public string Id { get; set; }
public string Item { get; set; }
}
在表单中设置ObservableCollection属性:
public ObservableCollection<MyClass> MyClassCollection { get; set; }
公共ObservableCollection MyClassCollection{get;set;}
然后将MyClassCollection属性设置为反序列化的json
MyClassCollection = JsonConvert.DeserializeObject<ObservableCollection<MyClass>>(e.Result);
MyClassCollection=JsonConvert.DeserializeObject(e.Result);
这样,您就可以将类列表绑定到XAML中的LongListSelector控件:
<phone:LongListSelector Name="list" ItemsSource="{Binding MyClassCollection}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding id}" />
<TextBlock Text=" ( " />
<TextBlock Text="{Binding item}" FontStyle="Italic" />
<TextBlock Text=" )" />
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
这应该行得通。不行。。。我不明白,我们应该使用list.itemsource吗?什么是可观察收集将有所帮助