0

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;
            }
6
  • What is your problem exactly? Are you getting any errors? To bind the List, is by setting the ItemsSource, which you are doing. Commented Oct 6, 2018 at 5:15
  • @BrunoCaceiro i'm confused how to bind a json array to xaml Commented Oct 6, 2018 at 5:17
  • You just say the ListView ItemsSource is a List of objects in your viewmodel. Commented Oct 6, 2018 at 5:21
  • @BrunoCaceiro i editted my post Commented Oct 6, 2018 at 7:12
  • Try to use ObservableCollection instead of List as mentioned here Commented Oct 6, 2018 at 9:01

2 Answers 2

1

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

hi the binding will be changed according to your field name
No i identified it, my problem is how to display a json array
it's still not displaying the array to listview, i tried your code
perhaps you are not changed the getstart() method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.