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.

6
  • 1
    Are you sure, you don't swallow an exception somewhere? That space seems suspicious: .../Covalience/Helpers/Samplejson .json" Commented Jan 17, 2022 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. Commented Jan 17, 2022 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. Commented Jan 17, 2022 at 12:28
  • i would say you are missing a step, take a look at this: stackoverflow.com/a/17788118/7968203 Commented Jan 17, 2022 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 Commented Jan 17, 2022 at 12:31

1 Answer 1

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; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Totally missed that those are fields, not props. Good catch!
That's a very good catch. I have changed them fields to properties and still can't read it.
@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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.