mardi 4 décembre 2018

generic api consume class in c#

try
            { 
                HttpResponseMessage httpResponseMessage = GlobalVariables.WebAPI.GetAsync("http://dummy.restapiexample.com/api/v1/employees").Result;
                if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
                {

                    var tupleList = new List<Tuple<dynamic, dynamic>>();
                    var genericList = JsonConvert.DeserializeObject<IEnumerable<object>>(httpResponseMessage.Content.ReadAsStringAsync().Result);
                    int _totalColumns = 0;
                    var allowFirst = false;
                    foreach ( var genericObject in genericList)
                    {
                        allowFirst = true;
                        if (allowFirst)
                        { 
                            try
                            {
                                Type type = genericObject.GetType();

                                // Get all public instance properties.
                                // Use the override if you want to classify
                                // which properties to return.
                                foreach (PropertyInfo info in type.GetProperties())
                                {  

                                }
                            }
                            catch (Exception)
                            {

                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception)
            {

            }

l dont know if this is possible but am trying to create a generic class to consume api.

the data will only be in form of array objects without inner objects on another object like sample

sample 1

 [
      {
        "id": "24",
        "employee_name": "Doris Wilder",
        "employee_salary": "85600",
        "employee_age": "23",
        "profile_image": "images/default_profile.png"
      },
      {
        "id": "25",
        "employee_name": "Angelica Ramos",
        "employee_salary": "1200000",
        "employee_age": "47",
        "profile_image": "images/default_profile.png"
      }
    ]

Sample Two

[
  {
    "project_Id": "24",
    "project_name": "Lorem Ipsum" 
  },
   {
    "project_Id": "25",
    "project_name": "Lorem Ipsum ipsum" 
  }
]

Sample 3

[
  {
    "Employee_Id": "25",
    "Employee_name": "Sam Doe" 
  },
{
    "Employee_Id": "2",
    "Employee_name": "Jon doe" 
  }
]

am stack on now determining the number of object propertise and how to store it ?

like sampe 1 has four properties

sample 2 has two properties how best can l do that and determining how to save it:





Aucun commentaire:

Enregistrer un commentaire