I have the following JSON:
{
"countries": [
{
"name": "Afghanistan",
"alternative_names": [],
"formal_name": "Islamic Republic of Afghanistan",
"the_prefix": false,
"in_un": true
}
]
}
The following classes:
[Serializable]
public class CountriesData
{
public List<CountryData> countries;
}
[Serializable]
public class CountryData
{
/// <summary>
/// The informal name of the country.
/// <example>
/// E.g. China
/// </example>
/// </summary>
public string name;
/// <summary>
/// A <c>List<string></c> containing other informal names for the country.
/// <example>
/// E.g. for Myanmar, an alternative name is Burma.
/// </example>
/// </summary>
public List<string> alternative_names;
/// <summary>
/// The formal name of the country.
/// <example>
/// E.g. People's Republic of China
/// </example>
/// </summary>
public string formal_name;
/// <summary>
/// Should the word "the" come before this country's informal name?
/// <example>
/// E.g. for China, Spain, France, no. For (the) Gambia, (the) United Kingdom, (the) United States of America, yes.
/// </example>
/// </summary>
public bool the_prefix;
/// <summary>
/// Is this country a UN member state?
/// <example>
/// E.g. for China, Spain, France, yes. For Kosovo, no.
/// </example>
/// </summary>
public bool in_un;
}
and I am running the line of code:
CountriesData data = JsonSerializer.Deserialize<CountriesData>(jsonString);
This is resulting in null
. I have tested and the string jsonString
does correctly contain above JSON as a string. No matter what I have tried, this always returns null
.
I am getting the warning Warning CS8601 Possible null reference assignment.
on this line of code.
Why is this not deserializing properly?