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.
From the C# Documentation :
So you can convert your fields to properties and it will serialize.
public class Books
{
[JsonProperty("Index Number")]
public string IndexNumber { get; set; }
[JsonProperty("Company Name")]
public string CompanyName { get; set; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.