以Xamarin形式解析API响应

时间:2017-10-29 15:15:45

标签: xamarin

我是xamarin的新手。我试图从php api获取数据,但它不会给我任何数据或错误,请检查。网址为
    http://mehdibalti.000webhostapp.com/xamrin/getall.php

response is .. [{"Id":"1","Name":"Mehdi","Department":"Balti"},{"Id":"2","Name":"Mehdi","Department":"Syntecx"}]

and i did like this 



  public class EmployeeServices
    {
        public EmployeeServices()
        {
        }


        public async Task<List<EmployeeModel>>  getEmployeeAsyn(){




            RestClient<EmployeeModel> resclient = new RestClient<EmployeeModel>();

            var list =await resclient.GetTodoItemsAsync();
            return list;

        }

    }
}

这是我的resClient类

        public async Task<List<EmployeeModel>> GetTodoItemsAsync()
        {

            var httpClient = new HttpClient();


            var response = await httpClient.GetStringAsync(getAllUrl);


            response = response.Replace("\"", "");


            var todoItems = JsonConvert.DeserializeObject<List<EmployeeModel>>(response);



            return todoItems;

        }

这是Model类  公共类EmployeeModel

{

    public string Id { get; set; }
    public string Name { get; set; }

    public string Department { get; set; }
}


but it did not return any list

1 个答案:

答案 0 :(得分:1)

结果API给出的不是列表。使用http://json2csharp.com/之类的内容 你要调查json。