我差不多尝试了2天,在这里和那里搜索下一个有组织的。 这是我的json示例(使用WebClient => DownloadStringCompletedEventHandler获取):
{"order_id":"12345678","itemList":["235724","203224","222224","222324","230021"],"amount":["65","50","10","25","42"]}
来自json2sharp:
public class RootObject
{
public string order_id { get; set; }
public List<string> itemList { get; set; }
public List<string> amount { get; set; }
}
我的Xaml:
<ListBox x:Name="MyListBox" Height="344" Margin="0,107,0,245">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding image}"/>
<TextBlock Text="{Binding order_id}"/>
<TextBlock Text="{Binding itemList}"/>
<TextBlock Text="{Binding amount}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我的cs:
private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
return;
}
RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);
List<RootObject> myItems = rootObject.itemList.;
foreach (var item in myItems)
{
MyListBox.Items.Add(item);
}
Order_id.Text = rootObject.order_id.ToString();
//MyListBox.ItemsSource = Root
//MyListBox.DataContext = RootObject;
}
我能够将字符串order_id显示为a,但我没有列出itemlist + amount。 有人能把我放在正确的方向吗? 提前谢谢。
答案 0 :(得分:0)
尝试:
<强> CS:强>
var result = "{'order_id':'12345678','itemList':['235724','203224','222224','222324','230021'],'amount':['65','50','10','25','42']}";
var rootObject = JsonConvert.DeserializeObject<RootObject>(result);
var items = new List<RootObject>
{
rootObject
};
MyListBox.ItemsSource = items;
<强> XAML:强>
<ListBox x:Name="MyListBox"
Margin="{StaticResource PhoneMargin}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock>
<TextBlock.Inlines>
<Run Text="order_id:" />
<Run Text="{Binding order_id}" />
</TextBlock.Inlines>
</TextBlock>
<StackPanel Orientation="Horizontal">
<TextBlock Text="ItemList: " />
<ItemsControl ItemsSource="{Binding itemList}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Amount: " />
<ItemsControl ItemsSource="{Binding amount}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>