Hello,
I still have a problem with loading the data. Can anyone tell me how to load data into the list?
I test your API, your Json data have only one record. And your Json properties are different from your TodoItem
.
Firstly, please change your name of property in the TodoItem
like following code.
public class TodoItem {
public int userId { get; set; }
public int id { get; set; }
public string title { get; set; }
public string body { get; set; }
}
After that, you will get some errors about Error CS1061 'TodoItem' does not contain a definition for 'ID'
, please change these properties from TodoItem
.
Then, open your public async Task<List<TodoItem>> RefreshDataAsync()
method in RestService.cs
. Based on your API, you just have only one record. You should change Items = JsonSerializer.Deserialize<List<TodoItem>>(content, _serializerOptions);
to
Items.Add(System.Text.Json.JsonSerializer.Deserialize<TodoItem>(content, _serializerOptions));
In the end, please change the binding value from name
to title
in your TodoListPage
<Label Text="{Binding title}" VerticalTextAlignment="Center" />
By the way, if you want to add other records in your API. Such as following Json.
[
{
"userId": 1, "id": 4, "title": "this is title1", "body": "test data"
},
{
"userId": 2, "id": 4, "title": "this is title2", "body": "test data"
}
]
You can use following code to deserialize JSON data in public async Task<List<TodoItem>> RefreshDataAsync()
method of RestService.cs
.
//if you have several records in API, you could deserialize to List<TodoItem>
Items=System.Text.Json.JsonSerializer.Deserialize<List<TodoItem>>(content, _serializerOptions);
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.