0

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.

Jas
  • 419
  • 1
  • 6
  • 14
  • 1
    Are you sure, you don't swallow an exception somewhere? That space seems suspicious: `.../Covalience/Helpers/Samplejson .json"` – Fildor Jan 17 '22 at 12:21
  • 1
    The name of the file also contains white space. I have fixed it and it still does not works. I will update the question also to avoid confusions. – Jas Jan 17 '22 at 12:25
  • @Jas I think the OP just wrote some pseudo code, not his actual code, so that's why you see the typos. But i guess even the pseudo version kinds of shows the gist. – Software Dev Jan 17 '22 at 12:28
  • i would say you are missing a step, take a look at this: https://stackoverflow.com/a/17788118/7968203 – Isparia Jan 17 '22 at 12:30
  • Just a suggestion for debugging : Instead of casting to any type, try to see if your stream-reader is properly reading the files. Tho I doubt my thought because if it wasn't able to locate/read the file, it would've broke there, instead of executing next lines – Software Dev Jan 17 '22 at 12:31
  • So I moved the file to some other location(D:file.json) and it started to read the data. I am still not sure what was wrong with the first path but strangely, there was no runtime error. Thank you all for your comments and suggestions. – Jas Jan 17 '22 at 12:45

1 Answers1

1

From the C# Documentation:

  • By default, fields are ignored. You can include field.
  • By default, all public properties are serialized.

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; }
}
Berkays
  • 364
  • 3
  • 10
  • 1
    Totally missed that those are fields, not props. Good catch! – Fildor Jan 17 '22 at 12:38
  • That's a very good catch. I have changed them fields to properties and still can't read it. – Jas Jan 17 '22 at 12:39
  • @Jas Just noticed that your path is using backslash instead of forward slash. Try with forward slashes. Are you sure you have the content as text after file read? – Berkays Jan 17 '22 at 12:45