C# Xamarin表单-如何在ListView中显示JSON数组?
我不知道如何向ListView显示JSON数组,有人能帮我吗? 我试过这些代码,但它不起作用 我的JSON数组C# Xamarin表单-如何在ListView中显示JSON数组?,c#,xaml,xamarin,xamarin.forms,xamarin.android,C#,Xaml,Xamarin,Xamarin.forms,Xamarin.android,我不知道如何向ListView显示JSON数组,有人能帮我吗? 我试过这些代码,但它不起作用 我的JSON数组 {"table":[ {"table_no":"1","order_status":"served"}, {"table_no":"2","order_status":"pending"}, {"table_no":"3","order_status":"served"}, {"table_no":"4","order_status":"served"
{"table":[
{"table_no":"1","order_status":"served"},
{"table_no":"2","order_status":"pending"},
{"table_no":"3","order_status":"served"},
{"table_no":"4","order_status":"served"},
{"table_no":"8","order_status":"served"},
{"table_no":"10","order_status":"served"},
{"table_no":"11","order_status":"served"},
{"table_no":"12","order_status":"served"},
{"table_no":"14","order_status":"pending"},
{"table_no":"16","order_status":"served"}]}
OrderStat.cs(如何绑定或反序列化它?)
公共类OrderStat
{
公共字符串表_no{get;set;}
公共字符串顺序_状态{get;set;}
}
公共类根对象
{
公共列表表{get;set;}
}
OrderStatus.xaml
<ListView x:Name="selectOrd" RowHeight="50" SeparatorColor="White"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<StackLayout Orientation="Horizontal" >
<StackLayout Orientation="Horizontal" VerticalOptions="Center">
<Image Source="table.png" Scale="1"/>
<Label Text="{Binding table_no,StringFormat=' Table no.{0:F0}'}" Font="30" TextColor="White" />
</StackLayout>
<StackLayout HorizontalOptions="FillAndExpand" x:Name="BG" VerticalOptions="Center" >
<Label Text="{Binding order_status}" Font="50" TextColor="White" FontAttributes="Bold" HorizontalTextAlignment="Center"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
OrderStatus.xaml.cs
private async void GetStat()
{
HttpClient client = new HttpClient();
var response = await client.GetStringAsync("http://ropenrom2-001-site1.etempurl.com/Restserver/index.php/customer/view_table_orders");
var stat = JsonConvert.DeserializeObject<List<RootObject>>(response);
selectOrd.ItemsSource = stat;
}
private async void GetStat()
{
HttpClient=新的HttpClient();
var response=wait client.GetStringAsync(“http://ropenrom2-001-site1.etempurl.com/Restserver/index.php/customer/view_table_orders");
var stat=JsonConvert.DeserializeObject(响应);
选择ord.ItemsSource=stat;
}
实际上,您没有定义选择顺序和对象
从JSON中检索应该是OrderStat
<StackLayout BackgroundColor="White">
<ListView x:Name="ListView1" RowHeight="60">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical" Padding="8,0,8,0">
<Label Text="{Binding ArticleTitle}" TextColor="#000" FontSize="14" LineBreakMode="TailTruncation" />
<Label Text="{Binding description}" TextColor="#000" LineBreakMode="TailTruncation" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
private async void GetStat()
{
HttpClient=新的HttpClient();
var response=wait client.GetStringAsync(“http://ropenrom2-001- site1.etempurl.com/Restserver/index.php/customer/view_table_orders);
var stat=JsonConvert.DeserializeObject(响应);
ListView1.ItemsSource=stat;
//选择ord.ItemsSource=stat;
}
实际上,您没有定义选择顺序和对象
从JSON中检索应该是OrderStat
<StackLayout BackgroundColor="White">
<ListView x:Name="ListView1" RowHeight="60">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical" Padding="8,0,8,0">
<Label Text="{Binding ArticleTitle}" TextColor="#000" FontSize="14" LineBreakMode="TailTruncation" />
<Label Text="{Binding description}" TextColor="#000" LineBreakMode="TailTruncation" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
private async void GetStat()
{
HttpClient=新的HttpClient();
var response=wait client.GetStringAsync(“http://ropenrom2-001- site1.etempurl.com/Restserver/index.php/customer/view_table_orders);
var stat=JsonConvert.DeserializeObject(响应);
ListView1.ItemsSource=stat;
//选择ord.ItemsSource=stat;
}
这是在ListView中显示json的正确方法。我也更新了你的Xaml。我刚刚从ListView中删除了白色文本颜色和x:Name
Xaml
这是在ListView中显示json的正确方法。我也更新了你的Xaml。我刚刚从ListView中删除了白色文本颜色和x:Name Xaml
你到底有什么问题?你有什么错误吗?绑定列表的方法是设置ItemsSource,您正在这样做。@BrunoCaceiro我不知道如何将json数组绑定到XAML您只需说ListView ItemsSource是viewmodel中的对象列表。@BrunoCaceiro我编辑了我的帖子,试图使用ObservableCollection而不是前面提到的List。您的问题到底是什么?你有什么错误吗?要绑定列表,请通过设置ItemsSource,您正在执行的操作。@BrunoCaceiro我不知道如何将json数组绑定到XAML您只需说ListView ItemsSource是viewmodel中的一个对象列表。@BrunoCaceiro我编辑了我的帖子,尝试使用ObservableCollection而不是前面提到的列表。绑定将根据您的字段名进行更改否我已识别它,我的问题是如何显示json数组它仍然没有将数组显示到listview,我尝试了你的代码也许你没有更改getstart()方法绑定将根据你的字段名更改否我识别了它,我的问题是如何显示json数组它仍然没有将数组显示到listview,我试过你的代码也许你没有改变getstart()方法
private async void GetStat()
{
HttpClient client = new HttpClient();
var response = await client.GetStringAsync("http://ropenrom2-001- site1.etempurl.com/Restserver/index.php/customer/view_table_orders");
var stat = JsonConvert.DeserializeObject<List<OrderStat>>(response);
ListView1.ItemsSource = stat;
// selectOrd.ItemsSource = stat;
}
<ListView x:Name="selectOrd" RowHeight="50" SeparatorColor="White"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" >
<StackLayout Orientation="Horizontal" VerticalOptions="Center">
<Image Source="table.png" Scale="1"/>
<Label Text="{Binding table_no,StringFormat='Table no.{0:F0}'}" Font="30" />
</StackLayout>
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="Center" >
<Label Text="{Binding order_status}" Font="50" FontAttributes="Bold" HorizontalTextAlignment="Center"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
[DataContract]
public class OrderStat
{
[DataMember]
public string table_no { get; set; }
[DataMember]
public string order_status { get; set; }
}
[DataContract]
public class RootObject
{
[DataMember]
public List<OrderStat> table { get; set; }
}
public MainPage()
{
InitializeComponent();
GetStat();
}
private async void GetStat()
{
HttpClient client = new HttpClient();
var response = await client.GetAsync("http://ropenrom2-001-site1.etempurl.com/Restserver/index.php/customer/view_table_orders");
var result = await response.Content.ReadAsStringAsync();
var serializer = new DataContractJsonSerializer(typeof(RootObject));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
var data = (RootObject)serializer.ReadObject(ms);
selectOrd.ItemsSource = data.table;
}