Home > Blockchain >  Deserialize inherited class from json api
Deserialize inherited class from json api

Time:11-13

I would like to deserialize an inherited class from json using Xamarin (C#)

classes:

public class AnnuaireTelephonique
{
    public string nom { get; set; }
    public string numero { get; set; }
}

public class RootAnnuaireTelephonique : List<AnnuaireTelephonique>
{
    public string nomCategorie { get; set; }
    public List<AnnuaireTelephonique> liste => this;
}

I use a herited class to display it then with a grouped listView But when I deserialize with the following code, it doesn't work and makes the app crash

var annuaireTelephoniqueJson = JsonConvert.DeserializeObject<List<RootAnnuaireTelephonique>>(ReadData(annuaireTelephoniqueFileName) /*correct json*/);
AnnuaireTelephonique = new ObservableCollection<RootAnnuaireTelephonique>(annuaireTelephoniqueJson);

ListView:

                <ListView ItemsSource=""
                          IsGroupingEnabled="true"
                          x:Name="listData"
                          HasUnevenRows="True"
                          SeparatorVisibility="None">
                    <ListView.GroupHeaderTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Label
                                    Text="{Binding nomCategorie}"
                                    FontSize="20"
                                    TextColor="Black"
                                    Margin="10"
                                    HorizontalOptions="Center"
                                    VerticalOptions="Start" />
                            </ViewCell>
                        </DataTemplate>
                    </ListView.GroupHeaderTemplate>

                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                    <customButton:CallNumberButton
                                        Text="{Binding nom}"
                                        NumberToCall="{Binding numero}"
                                        FontSize="15"
                                        Margin="8"
                                        TextColor="Black"
                                        BackgroundColor="#E6E6E6"
                                        VerticalOptions="Start"
                                    />
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

Json exemple:

[ { "nomCategorie": "1", "liste": [ { "nom": "A", "numero": "0600000000" }, { "nom": "B", "numero": ""0600000001" }, { "nom": "C", "numero": "0600000002" } ] }, { "nomCategorie": "2", "liste": [ { "nom": "D", "numero": "0600000003" }, { "nom": "E" "numero": "0600000004" } ] } ]

CodePudding user response:

try this

public ObservableCollection<AnnuaireTelephonique> AnnuaireTelephonique   { get; set; }

var annuaireTelephoniqueList = 
JsonConvert.DeserializeObject<List<RootAnnuaireTelephonique>>(ReadData(annuaireTelephoniqueFileName);

AnnuaireTelephonique  = 
new ObservableCollection(annuaireTelephoniqueList[0].AnnuaireTelephonique);

you have to create classes (don't make inheritance!)

 public class RootAnnuaireTelephonique
    {
        public string nomCategorie { get; set; }
        public List<AnnuaireTelephonique> AnnuaireTelephonique { get; set; }
    }
    public class AnnuaireTelephonique
    {
        public string nom { get; set; }
        public string numero { get; set; }
    }

and screen

ItemsSource="{Binding AnnuaireTelephonique}" 
  • Related