简体   繁体   中英

Xamarin Forms - how to display JSON array in ListView?

How can I display JSON array to ListView, I'm confused can somebody help me? I tried these code and it's not working.

my JSON array

 {"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 ( How do i bind this or how do i deserialize it? )

  public class OrderStat
    {
        public string table_no { get; set; }
        public string order_status { get; set; }
    }

    public class RootObject
    {
        public List<OrderStat> table { 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;
            }

Hi Actual you do not define the select order and the also the objects retrieve from the JSON should be OrderStat .below is the edited code

<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 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;
            }

This is the right way to show the json in ListView. I've also updated your Xaml too. I just removed the white text color and x:Name from your ListView.

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" />
                        </StackLayout>
                        <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="Center"  >
                            <Label  Text="{Binding order_status}" Font="50" FontAttributes="Bold" HorizontalTextAlignment="Center"/>
                        </StackLayout>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

OrderStat Class

[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; }
}

MainPage.xaml.cs

 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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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