C
C#7d ago
Theos

Issue when deserializing web response

Hey, I'm making a simple call to the api and looking at the text reponse it looks correct (check first image).
HttpResponseMessage response = await client.GetAsync(nextPageUrl);
string responseBody = await response.Content.ReadAsStringAsync();

if (response.IsSuccessStatusCode)
{
var animeListResponse = JsonSerializer.Deserialize<AnimeListResponse>(responseBody,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

if (animeListResponse.Data != null)
{
animeList.AddRange(animeListResponse.Data);
}
HttpResponseMessage response = await client.GetAsync(nextPageUrl);
string responseBody = await response.Content.ReadAsStringAsync();

if (response.IsSuccessStatusCode)
{
var animeListResponse = JsonSerializer.Deserialize<AnimeListResponse>(responseBody,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

if (animeListResponse.Data != null)
{
animeList.AddRange(animeListResponse.Data);
}
For some reason ListStatus is always null (look at second image)
public class AnimeListResponse
{
public AnimeData[] Data { get; set; }
public Paging Paging { get; set; }
}

public class AnimeData
{
public AnimeNode Node { get; set; }
public ListStatus ListStatus { get; set; }
}

public class AnimeNode
{
public int Id { get; set; }
public string Title { get; set; }
}

public class ListStatus
{
[JsonPropertyName("status")]
public string Status { get; set; }

[JsonPropertyName("score")]
public int Score { get; set; }

[JsonPropertyName("num_episodes_watched")]
public int Num_Episodes_Watched { get; set; }
}
public class AnimeListResponse
{
public AnimeData[] Data { get; set; }
public Paging Paging { get; set; }
}

public class AnimeData
{
public AnimeNode Node { get; set; }
public ListStatus ListStatus { get; set; }
}

public class AnimeNode
{
public int Id { get; set; }
public string Title { get; set; }
}

public class ListStatus
{
[JsonPropertyName("status")]
public string Status { get; set; }

[JsonPropertyName("score")]
public int Score { get; set; }

[JsonPropertyName("num_episodes_watched")]
public int Num_Episodes_Watched { get; set; }
}
No description
No description
14 Replies
glhays
glhays7d ago
Try adding the Attribute to AnimeData.
[JsonPropertyName("list_status")]
public ListStatus ListStatus { get; set; }
[JsonPropertyName("list_status")]
public ListStatus ListStatus { get; set; }
Theos
TheosOP7d ago
yep, issue solved, thanks! That's why I hate dealing with json I always will make one issue like this
leowest
leowest7d ago
@Theos I PRESENT U, https://app.quicktype.io/
Instantly parse JSON in any language | quicktype
Whether you're using C#, Swift, TypeScript, Go, C++ or other languages, quicktype generates models and helper code for quickly and safely reading JSON in your apps. Customize online with advanced options, or download a command-line tool.
leowest
leowest7d ago
json in, c# out
Theos
TheosOP7d ago
bookmarked best thing i've seen today
leowest
leowest7d ago
$close
MODiX
MODiX7d ago
If you have no further questions, please use /close to mark the forum thread as answered
Theos
TheosOP7d ago
wrong one
leowest
leowest7d ago
what is?
Theos
TheosOP7d ago
No description
leowest
leowest7d ago
guess bot is broken let me try yep bot is broken just ignore for now
Theos
TheosOP7d ago
oke :Ok:
Unknown User
Unknown User7d ago
Message Not Public
Sign In & Join Server To View
Mayor McCheese
Also don't keep remaking json serializer options, you should be reusing that

Did you find this page helpful?