繁体   English   中英

Json.NET和RESTSharp-转换值时出错

[英]Json.NET and RESTSharp - Error Converting Value

我创建了一个API,现在尝试通过另一个应用程序进行访问。 即使我使用Newtonsoft.JSON对JSON进行序列化和反序列化,也遇到了以下InnerException转换错误:

{"Could not cast or convert from System.String to System.Collections.Generic.List`1[MidamAPI.Models.ManagerDTO]."}

以下是引发错误的代码段(在RESTFactory中):

     public List<T> Execute<T>(String endPoint) where T : new() {
        RestClient client = new RestClient(BASE_URL);

        IRestRequest req = new RestRequest(endPoint);
        req.AddHeader("Authorization", "Bearer " + this._token);
        var response = client.Execute(req).Content;
        List<T> responseData = JsonConvert.DeserializeObject<List<T>>(response);

        return responseData;
    }

在这里称为:

{
        RegisterViewModel vm = new RegisterViewModel();
        RESTFactory factory = new RESTFactory((String)Session["bearer"]);
        vm.availableManager = factory.Execute<ManagerDTO>("managers");
        vm.availableProperties = factory.Execute<PropertyDTO>("locations");
        vm.availableTrainers = factory.Execute<TrainerDTO>("trainers");

        return View(vm);
    }

该信息来自以下API控制器操作(在单独的应用程序中):

    [LocalFilter]
[RoutePrefix("managers")]
public class ManagersController : ApiController
{
    UnitOfWork worker = new UnitOfWork();

       [Route("")]
    public string Get()
    {
        IEnumerable<ManagerDTO> dtoList = Mapper.Map<List<Manager>, List<ManagerDTO>>(worker.ManagerRepo.Get().ToList());

        foreach (ManagerDTO dto in dtoList)
        {
            Manager manager = worker.ManagerRepo.GetByID(dto.ID);
            dto.Stores = (Mapper.Map<IEnumerable<Property>, IEnumerable<PropertyDTO>>(manager.Properties)).ToList();
        }

        return JsonConvert.SerializeObject(dtoList);
    }

两种应用中的DTO相同:

{
public class ManagerDTO
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public List<PropertyDTO> Stores { get; set; }
}




  public class PropertyDTO
{
    public int ID;
    public string ShortName { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Phone { get; set; }
    public string Lat { get; set; }
    public string Long { get; set; }
    public string GooglePlaceID { get; set; }
    public string PropertyNumber { get; set; }
    public int ManagerID { get; set; }
    public int TrainerID { get; set; }

    public string ManagerName { get; set; }
    public string ManagerEmail { get; set; }
    public string TrainerEmail { get; set; }

}

我尝试将json2csharp工具与响应一起使用,一切似乎对我来说都很好:

public class Store
{
    public int ID { get; set; }
    public string ShortName { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Phone { get; set; }
    public string Lat { get; set; }
    public string Long { get; set; }
    public string GooglePlaceID { get; set; }
    public string PropertyNumber { get; set; }
    public int ManagerID { get; set; }
    public int TrainerID { get; set; }
    public string ManagerName { get; set; }
    public string ManagerEmail { get; set; }
    public object TrainerEmail { get; set; }
}

public class RootObject
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public List<Store> Stores { get; set; }
}

任何意见,将不胜感激。

编辑:

RegisterViewModel类公共类RegisterViewModel {

    public RegistrationDTO regModel { get; set; }
    public List<ManagerDTO> availableManager { get; set; }
    public List<PropertyDTO> availableProperties { get; set; }
    public List<TrainerDTO> availableTrainers { get; set; }
}

使您的控制器的Get()返回类型为IHttpActionResult

并返回Ok(dtoList);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM