Консольное приложение - Как получить доступ к свойствам в JSON? - PullRequest
0 голосов
/ 12 марта 2020

Мне нужно получить доступ к свойствам json, чтобы я мог отфильтровать результат по регистрам устройств (AEVL2020 или AEVL2021), но я не могу понять, как получить к нему доступ. Кажется, что RootObject не может получить доступ к чему-либо. Я делаю это около 2 дней, поэтому мне нужна помощь.

Вот мой 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"
            }
        ]
    }
}

мой C#:

static void Main(string[] args)
{
    using (StreamReader r = new StreamReader("C:\\Users\\Admin\\source\\repos\\Practice1\\Practice1\\company1.json"))
    {
        string json = r.ReadToEnd();
        var root = JsonConvert.DeserializeObject<RootObject>(json);

        // This is where the error, I can't access either AEVL2020 or AEVL2021
        foreach (var p in root.AEVL2020 || root.AEVL2021)
        {
            Console.WriteLine(p.user_id + p.name + p.employee_id + p.privilege);
        }
    }
}

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

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

public class Registers
{
    public List<AEVL2020> AEVL2020 { get; set; }
    public List<AEVL2021> AEVL2021 { get; set; }
}

public class RootObject
{
    public Registers registers { get; set; }
}

Ответы [ 2 ]

1 голос
/ 12 марта 2020

Вы можете определить один AEVL класс для AEVL2020 и AEVL2020 следующим образом:

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

public class Registers
{
    public List<AEVL> AEVL2020 { get; set; }
    public List<AEVL> AEVL2021 { get; set; }
}

Тогда ваш код будет таким, и вы сможете получать элементы на основе заданной строки:

var aevlProp = root.registers.GetType().GetProperty("AEVL2020");
var values = (aevlProp.GetValue(root.registers, null) as List<AEVL>);

foreach (var p in values)
{
    Console.WriteLine(p.user_id + p.name + p.employee_id + p.privilege);
}

Выход для GetProperty("AEVL2020");

1Juan Dela Cruz120
2Pedro Dela Cruz320

Выход для GetProperty("AEVL2021");

1Maria Del Mundo290
2Jay Del Mundo2220
0 голосов
/ 12 марта 2020

Как-то так, я думаю

Учитывая

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

public class RootObject
{
   public Dictionary<string, List<SomeObject>> registers { get; set; }
}

Использование

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

var r= JsonConvert.DeserializeObject<RootObject>(input);

// choose the name you want here
var values = r.registers["<Name>"];

foreach (var p in values )
  Console.WriteLine(p.user_id + p.name + p.employee_id + p.privilege);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...