I am attempting to deserialize a json
file into a List of Objects. The JSON
file requires that I use UTF8
. I am attempting to do it this way below.
FileStream reader1 = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader streamReader1 = new StreamReader(reader1, Encoding.UTF8);
string jsonString1 = streamReader1.ReadToEnd();
byte[] byteArray1 = Encoding.UTF8.GetBytes(jsonString1);
MemoryStream stream1 = new MemoryStream(byteArray1);
DataContractJsonSerializer inputSerializer2;
inputSerializer2 = new DataContractJsonSerializer(typeof(List<Country>));
c = (List<Country>)inputSerializer2.ReadObject(stream1);
stream1.Close();
This actually reads the file into the list but only saves nulls and 0s.
Is this correct? All the references are in the right place from the looks of it. Is it just my toString()
on my classes that are messed up?
Thank you.
edit: Below is my class and its member variables that I am trying to deserialize into along with a snippet of the JSON and my toString override.
[DataContract]
public class Country
{
#region Member Variables
private string m_name;
private string m_capital;
private string m_region;
private string m_subRegion;
private int m_population;
private List<Currency> m_currency = new List<Currency>();
private List<Language> m_language = new List<Language>();
//Rest of class here
public override string ToString()
{
return "The country name is " + Name + "\n" + "The country capital is " + Capital + "\n" + "The country region is " + Region
+ "\n" + "The country sub region is " + SubRegion + "\n" + "The country population is " + Population + "\n"
+ "The country currency is " + Currencies + "\n" + "The country language is " + Languages;
}
/--------------------------------------------------------------/
[
{
"currencies": [
{
"code": "AFN",
"name": "Afghan afghani",
"symbol": "؋"
}
],
"languages": [
{
"iso639_1": "ps",
"iso639_2": "pus",
"name": "Pashto",
"nativeName": "پښتو"
},
{
"iso639_1": "uz",
"iso639_2": "uzb",
"name": "Uzbek",
"nativeName": "Oʻzbek"
},
{
"iso639_1": "tk",
"iso639_2": "tuk",
"name": "Turkmen",
"nativeName": "Türkmen"
}
],
"name": "Afghanistan",
"capital": "Kabul",
"region": "Asia",
"subregion": "Southern Asia",
"population": 27657145
}
]