Oscar Perez
Oscar Perez

Reputation: 103

I'm trying to read a list from an Rest API

I'm trying to read a list from a rest API i Created in .net

Here is the class

public class Cliente
    {
        public string Cliente1 { get; set; }
        [PrimaryKey]
        public string Correo { get; set; }
        public string Telefono { get; set; }
        public string Calle { get; set; }
        public string Hab { get; set; }
        public string Ciudad { get; set; }
        public string Estado { get; set; }
        public Nullable<int> Zip { get; set; }
        public int Row { get; set; }
        public string Celular { get; set; }
        public string ID { get; set; }
    }



here is the call

public Cliente getCliente()
        {
            try
            {
                Cliente cliente;
                var URLWebAPI = "https://www.caja.somee.com/api/Clientes";
                using (var Client = new System.Net.Http.HttpClient())
                {
                    var JSON = Client.GetStringAsync(URLWebAPI);
                    cliente = Newtonsoft.Json.JsonConvert.DeserializeObject<Cliente>(JSON.Result);
                }

                return cliente;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

here is the method

Repositorio repo = new Repositorio();
            Cliente listacliente = repo.getCliente();
            ListaClientes.ItemsSource = listacliente.Cliente1;

here is the xaml view

<ListView x:Name="ListaClientes" BackgroundColor="White" VerticalOptions="Fill" Grid.Row="1" Grid.Column="0" SeparatorColor="LightGray">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="20"/>
                                            <RowDefinition Height="20"/>
                                            <RowDefinition Height="20"/>
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="3*"/>
                                            <ColumnDefinition Width="7*"/>
                                        </Grid.ColumnDefinitions>
                                        <Label Text="{Binding Cliente1}"  Grid.Row="0" Grid.Column="1" HorizontalOptions="Center" VerticalOptions="Center"   TextColor="DarkRed"  />

                                    </Grid>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

and this is the error

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'AppCaja.Cliente' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.'

i really need your help, i woul like to make it works

Upvotes: 1

Views: 505

Answers (1)

  1. The API is returning XML, you need to update this.

  2. You need configure your client to read Json.

>

using (var Client = new System.Net.Http.HttpClient())
{
 //This line
 Client.DefaultRequestHeaders.Accept.Add(new 
 MediaTypeWithQualityHeaderValue("application/json"));    

 var JSON = Client.GetStringAsync(URLWebAPI);
 cliente = Newtonsoft.Json.JsonConvert.DeserializeObject<Cliente>(JSON.Result);
}

Upvotes: 1

Related Questions