0

I am working with json and I want to output on console the arrays of AEVL2020 or AEVL2021 but, I got an error "Object reference not set to an instance of an object". I don't know what cause of this and what instance am I missing? I followed this question how to work with json object in c# but it seems don't work much.

Here is my json:

{
    "registers":
    {
        "AEVL2020": [
            {
                "user_id": "1",
                "employee_id": "12",
                "name": "Juan Dela Cruz",
                "privilege": "0"
            },
            {
                "user_id": "2",
                "employee_id": "32",
                "name": "Pedro Dela Cruz",
                "privilege": "0"
            }
        ],
        "AEVL2021": [
            {
                "user_id": "1",
                "employee_id": "29",
                "name": "Maria Del Mundo",
                "privilege": "0"
            },
            {
                "user_id": "2",
                "employee_id": "222",
                "name": "Jay Del Mundo",
                "privilege": "0"
            }
        ]
    }
}

my C#:

static void Main(string[] args)
{
    using (StreamReader r = new StreamReader("C:\\Users\\Admin\\source\\repos\\Practice1\\Practice1\\company1.json"))
    {
        string json = r.ReadToEnd();
        RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);
        foreach (var registerDevices in obj.AEVL2020)
        { 
            Console.WriteLine(registerDevices.user_id + registerDevices.employee_id + registerDevices.name + registerDevices.privilege);
        }

        foreach (var registerDevices in obj.AEVL2021)
        {
            Console.WriteLine(registerDevices.user_id + registerDevices.employee_id + registerDevices.name + registerDevices.privilege);
        }

    }
}

public class CompanyRegisters
{
    public int user_id { get; set; }
    public int employee_id { get; set; }
    public string name{ get; set; }
    public int privilege { get; set; }
}

public class RootObject
{
    public List<CompanyRegisters> AEVL2020 { get; set; }
    public List<CompanyRegisters> AEVL2021 { get; set; }
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • Your RootObject doesn't match your JSON. – ProgrammingLlama Mar 12 '20 at 02:34
  • 1
    JSON is a really easy-to-understand format. Where you see an object in JSON (denoted by `{` and `}`), you need a class in C#. Where you see see an array in JSON (denoted by `[` and `]`), you need a collection type (e.g. List, Array, etc.) in C#. Where you see a property in JSON (e.g. `"name": "John"`) you need a corresponding property in your C# class. Currently your C# "root object" doesn't represent the outermost object of your JSON. Your "root object" class is actually the "registers" property of your JSON's root object. – ProgrammingLlama Mar 12 '20 at 02:36

1 Answers1

1

Your Root doesn't match the Json. It should look like following.

public class CompanyRegisters
{
    public int user_id { get; set; }
    public int employee_id { get; set; }
    public string name{ get; set; }
    public int privilege { get; set; }
}
public class RootObject
{
    public Registers registers{get;set;}
}
public class Registers
{
    public List<CompanyRegisters> AEVL2020 { get; set; }
    public List<CompanyRegisters> AEVL2021 { get; set; }
}
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51