C
C#2y ago
skyferian

maui deserialize problem

I am having a problem when trying to deserialize api data
C#
string content = await response.Content.ReadAsStringAsync();
Debug.WriteLine("content -->");
Debug.WriteLine(content);

mylists = JsonSerializer.Deserialize<List<Item>>(content, _jsonSerializerOptions);
Debug.WriteLine(mylists);
C#
string content = await response.Content.ReadAsStringAsync();
Debug.WriteLine("content -->");
Debug.WriteLine(content);

mylists = JsonSerializer.Deserialize<List<Item>>(content, _jsonSerializerOptions);
Debug.WriteLine(mylists);
// this is what content says [{"id":1,"name":"Paprika chips","brand":"Lays","store":"Aldi"},{"id":2,"name":"producttest","brand":"brandttest","store":"Lidl"} so this one is correct but only the deserialize gives me some kind of problem and i do not understand why this is what mylists says System.Collections.Generic.List`1[Boodschappenlijst.Model.Item]
C#
public class Item
{
public int Id { get; set; }
public string name { get; set; }
public string brand { get; set; }
public string store { get; set; }
}
C#
public class Item
{
public int Id { get; set; }
public string name { get; set; }
public string brand { get; set; }
public string store { get; set; }
}
10 Replies
sibber
sibber2y ago
JsonSerializer is case sensitive by default also .ToString() returns the type by default it doesnt return the contents of the list you can use string.Join for that or even better, use the debugger
skyferian
skyferianOP2y ago
SO bassicaly the I in Id and what do you mean with use the debugger
sibber
sibber2y ago
put a breakpoint after deserializing and check the contents of list rather than logging
skyferian
skyferianOP2y ago
oh okey
sibber
sibber2y ago
JsonSerializerOptions.PropertyNamingPolicy Property (System.Text.Json)
Gets or sets a value that specifies the policy used to convert a property's name on an object to another format, such as camel-casing, or null to leave property names unchanged.
sibber
sibber2y ago
so you can use PascalCase for the class properties in c#
skyferian
skyferianOP2y ago
i see now bassicaly i was just printing the type instead of the data
sibber
sibber2y ago
yup also use PascalCase for propertie names :)
skyferian
skyferianOP2y ago
thank you for your help and yes i forgot you are right
sibber
sibber2y ago
np
Want results from more Discord servers?
Add your server