Share via

c# Collection, Databinding & JSON question

kiwis 1 Reputation point
2022-12-30T03:45:12.797+00:00

So, I'm retrieving a JSON file from my website. I'm trying to bind it to a XAML CollectionView on my .net maui application.

I have a Player class and a PlayerList class.

In debug jsonPLayerList has all my players. I can't attach it to my Collection view? How can I do this?

On my JsonSerializer.Deserialize<PlayerLists>(result); line, what does the object inside the <> do, I can't see that in the docs for it? https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializer.deserialize?view=net-7.0

I'm very lost.

var targetAdd = new Uri("https://myfootballclub.com/players.php");             
var result = client.GetAsync(targetAdd).Result.Content.ReadAsStringAsync().Result.ToString();  
      
//var jsonPlayerList = JsonSerializer.Deserialize<List<Player>>(result);  
var jsonPlayerList = JsonSerializer.Deserialize<PlayerLists>(result);  
      
var collectionView = new CollectionView();  
collectionView.ItemsSource = jsonPlayerList;  
      
collectionView.SetBinding(jsonPlayerList, "Player");  

Here is my PlayerList class,

public class PlayerLists  
{  
    public List<Player> PlayerList = new List<Player>();  
}  

And my player class

    public class Player  
    {  
        public int PlayerId { get; set; }  
        public string PlayerSurname { get; set; }  
        public string PlayerFirstname { get; set; }  
}  
Developer technologies | .NET | .NET Multi-platform App UI
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


2 answers

Sort by: Most helpful
  1. kiwis 1 Reputation point
    2022-12-30T20:08:48.257+00:00

    Perfect thanks team.

    I still struggle to read the MSDN.

    var list = JsonSerializer.Deserialize<List<Player>>(result);  
    var collectionView = new CollectionView();  
    collectionView.ItemsSource = list;  
    collectionView.SetBinding(ItemsView.ItemsSourceProperty, "Player");  
    

    I went with the above code, I'm not sure where on the MSDN this scenario is covered, I see loads of TValue options? What is TValue? I can't see, TValue, inside each other?

    https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializer.deserialize?view=net-7.0

    My second question is, this doesn't load all my players onto my page when I run it. In debug my list has all my players. What's wrong?

    <CollectionView x:Name="collectionView">

    <!--  
    <CollectionView.ItemsSource>  
        <x:Array Type="{x:Type model:Player}">  
            <model:Player PlayerFirstname="Test"/>  
            <model:Player PlayerFirstname="Test"/>  
            <model:Player PlayerFirstname="Test"/>  
            <model:Player PlayerFirstname="Test"/>  
              
        </x:Array>  
    </CollectionView.ItemsSource>  
    -->  
    <CollectionView.ItemTemplate>  
        <DataTemplate x:DataType="model:Player">  
            <HorizontalStackLayout Padding="10" Spacing="10">  
                <Label Text="{Binding PlayerFirstname}" FontSize="22" />  
            </HorizontalStackLayout>  
        </DataTemplate>  
    </CollectionView.ItemTemplate>  
    </CollectionView>  
    

    Was this answer helpful?


  2. Bruce (SqlWork.com) 84,061 Reputation points
    2022-12-30T17:09:44.697+00:00

    To deserialize a json string to c# object, the library needs to know the type, that is what in the <>, as the deserialization method is a generic. The json needs to match the properties of the type.

    {  
          “PlayerList”:[   
               {  
                    “PlayerId”: 1,  
                     PlayerSurname“: “test”  
                },  
                {  
                    “PlayerId”:  2  
                     PlayerSurname“: “test2”  
                }  
            ]  
     }  
      
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.