Generate dynamic Class Object Instances from dynamic JSON string at runtime

450 Views Asked by At

I have a problem with dynamic objects. Here's my script: I have table of content on PostgreSql(version 13). There is a json type field on this table. The interior of this space is completely dynamic. So it doesn't always have the same json structure.

What I want to do is to filter the contents according to the dynamic fields inside the json field. I did this. But what I can't do is: I want to send this content to my razor view. While making this post, I want to convert this json field into a c# object. In other words, it should always return to the razor view as an object that takes this json, with its data in it. Let me give an example.

Id  |  Title            |  DynamicData
-------------------------------------------------------------------
1   |  Content1         | {"Name": "Serkan", "Surname": "Ateş", Details: { "Job": "Programmer", "Age": 24, IsMale: true, Birthday: "2021-08-29 12:54:37" }}
2   |  Content2         | {"Name": "Halil", "Surname": "Kazan", Details: { "Job": "Teacher", "Age": 32, IsMale: true, Birthday: "2022-08-29 12:54:37" }}
3   |  Content3         | {"SlideName": "Blabla", "Order": 1, "ImagePath": "../Images/Slide1.jpg"}
4   |  Content4         | {"SlideName": "Blabla 2", "Order": 3, "ImagePath": "../Images/Slide2.jpg"}

As you can see the structure of my "DynamicData" field is variable. How can I convert the DynamicData field to an object when I pull my data from this table? There may also be hundreds of records in this table. Therefore, performance will be very important for me when converting objects into dynamic objects. According to this table, if I return only 1 and 2 Id records, I want to create a class structure like this and fill its instance with data and return it.


    public class Details
    {
        public string Job { get; set; }
        public int Age { get; set; }
        public bool IsMale { get; set; }
        public string Birthday { get; set; }
    }
    public class Person
    {
        public string Name { get; set; }
        public string Surname { get; set; }
        public Details Details { get; set; }
    }
    public class Content
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public Person DynamicData { get; set; }
    }

    var contents = helper.GetContents(...);
    //2 Content type record should be returned

I hope I was able to explain my problem. I tried to do it with ExpandoObject but it doesn't quite meet my requirements. I want to take instances of the classes I created at runtime, not dynamically, and send them to the view as a model. Please help me :( Thank you from now.

Edit:

enter image description here

Friends, when I try to do what I want to do with dynamic type, if my dynamic object is an array, I cannot access properties while accessing the elements of that array in a loop. But when I debug, if I watch directly, I can access the values of the object. What is the reason of this?

enter image description here

0

There are 0 best solutions below