I am trying to read the data from a json file. The file is stored locally and the format data in this file looks like this.
{
"DataToDisplay": [
{
"Index Number": "9788189999599",
"Company Name": "TALES OF SHIVA"
},
{
"Index Number": "9788189999599",
"Company Name": "TALES OF SHIVA"
},
{
"Index Number": "9788189999599",
"Company Name": "TALES OF SHIVA"
}
]
}
I am using following code to read the data. The class structure goes like this.
public class DataToDisplayClass
{
[JsonProperty("DataToDisplay")]
public List< Books> DataToDisplay { get; set; }
}
public class Books
{
[JsonProperty("Index Number")]
public string IndexNumber;
[JsonProperty("Company Name")]
public string CompanyName;
}
And I am trying to read code as below.
using (StreamReader file = File.OpenText(@"C:/Users/ravin/source/repos/Covalience/Helpers/Samplejson.json"))
{
JsonSerializer serializer = new JsonSerializer();
DataToDisplayClass _listOfBooks = (DataToDisplayClass)serializer.Deserialize(file, typeof(DataToDisplayClass));
}
The object "_listOfBooks" is coming up as null. Not sure what I am doing wrong. Is it the class structure or the way I am reading the data.
[update] Removed white space from file name.