C#中的通用API消耗类

时间:2018-12-04 10:33:50

标签: c# json reflection

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)
            {

            }

我不知道这是否可行,但正在尝试创建一个通用类来使用api。

数据将仅以数组对象的形式出现,而没有内部对象出现在另一个对象(如样本)上

示例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"
      }
    ]

示例二

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

示例3

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

最多

Sample 20++   ..................:

m现在确定对象属性的数量以及如何存储它?

像样本1一样具有四个属性

样本2具有两个属性,我该怎么做? 并确定如何保存它:

编辑

我有超过20个网址{并且可能会增加}是我不希望创建的对象的不同数组,不是要创建强类型类,因为数据仅用于呈现在html表字符串上,但是所有api值都会拥有0-100个对象数组,因此所有l想要知道它的可能还是l会坚持@nkosi所说的l已经在做的事情

0 个答案:

没有答案