【问题标题】:Json.NET and RESTSharp - Error Converting ValueJson.NET 和 RESTSharp - 转换值时出错
【发布时间】:2017-10-31 13:53:19
【问题描述】:

我创建了一个 API,我现在正尝试通过另一个应用程序访问它。尽管我使用 Newtonsoft.JSON 来序列化和反序列化 JSON,但我遇到了转换错误,并出现以下 InnerException:

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

以下是抛出错误的sn-p(在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; }
}

【问题讨论】:

  • 我们可以看看RegisterViewModel是什么样子的吗?
  • 当然,模型已添加。谢谢。
  • API 返回的 JSON 格式是否正确?
  • JSON 显示正确。 json2charp 工具能够适当地生成类,并且我能够通过 PHP 和 Javascript 客户端访问 api。

标签: c# json json.net


【解决方案1】:

让你的控制器 Get() 返回类型 IHttpActionResult

然后返回 好的(dtoList);

【讨论】:

  • 谢谢。我试了一下,不幸的是它仍然返回同样的错误。
  • 对不起,您不需要对对象进行序列化,只需返回 return Ok(dtoList);
猜你喜欢
  • 2013-05-15
  • 1970-01-01
  • 2014-07-27
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多