Click here to Skip to main content
15,888,590 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, everyone. I work with json object in my c# application. I deserialized the json successfully and what I want is to take only meaningful properties (properties with actual values) from the json and to use them (actually I have to update database tables using these properties).

What I have tried:

For example let's see "TicketsData" class. Here it is:

C#
public class TicketsData
    {
        public int id { get; set; }
        public string ticket_number { get; set; }
        public int user_id { get; set; }
        public int? origin_id { get; set; }
        public int priority_id { get; set; }
        public int sla_plan_id { get; set; }
        public int status_id { get; set; }
        public int? timezone_id { get; set; }
        public string is_auto_assigned { get; set; }
        public string created_at { get; set; }
        public string updated_at { get; set; }
        public int? created_by { get; set; }
        public string closed_at { get; set; }
        public string subject { get; set; }
        public string first_assigned_agent_or_team { get; set; }
        public string last_assigned_agent_or_team { get; set; }
        public string service_fulfill { get; set; }
        public int? no_of_workflow_positions { get; set; }
        public double? invoice_amount { get; set; }
        public object invoiceDetails { get; set; }
    }


Part of the json dealing with "TicketsData":

ticketsData
5
updated_at "2018-09-20 14:21:40"
29 {…}

I use reflection in my code:

C#
public void UpdateEntriesFrom_tbl_ts_TicketsData(Dictionary<int, TicketsData> ticketsData)
        {
            foreach (KeyValuePair<int, TicketsData> entry in ticketsData)
            {
                Type t = entry.Value.GetType();
                PropertyInfo[] props = t.GetProperties();
                Dictionary<string, object> dict = new Dictionary<string, object>();
                foreach (PropertyInfo prp in props)
                {
                    object value = prp.GetValue(entry.Value, new object[] { });
                    dict.Add(prp.Name, value);
                }

                foreach (var item in dict)
                {
                        if (item.Value != null)
                        {                                                       
                        UpdateEntry("tbl_ts_TicketsData", item.Key, item.Value,
entry.Key);
                        }   
                }
            }
}

The problems I am facing are two. First problem is that the reflection gets all properties of "TicketsData" class including the "id" and what I need is all properties without "id". The second problem is that if I use the above code, I make a check:

C#
if (item.Value != null)
{ 
UpdateEntry("tbl_ts_TicketsData", item.Key, item.Value,
entry.Key);
}


but in my db table "tbl_ts_TicketsData" there are fields like "user_id" which are (int, not null) and when json is as the above snippet, "user_id" comes with default value of "0", passes the check

C#
if (item.Value != null)


and triggers the update method, which is not what I want (I want to use only the meaningful fields from the json). I tried another approach without reflection like this:

C#
foreach (KeyValuePair<int, TicketsData> entry in ticketsData)
{
         if (entry.Value.user_id != 0)
         {
                    UpdateEntry("tbl_ts_TicketsData", "user_id", entry.Value.user_id, entry.Key);
         }
}

but the problem with that approach is thah "0" is a valid digit for "id" column of "User" table and I can miss a case if I use the code above.
Can you help me to get only the meaningful properties (properties with real not defalut values) from the deserialized json so I to be able to use them?
Posted
Updated 21-Sep-18 5:01am
v2
Comments
Richard MacCutchan 21-Sep-18 9:44am    
How? Only you can decide what is a meaningful value.
Graeme_Grant 21-Sep-18 18:19pm    
The problem that you have is not related to JSON deserialization, it is to do with your logic for db storage.

If you're going to use reflection why not use Entity Framework? They have solved the problems that you are experiencing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900