如何将JSON响应绑定到ASP MVC中的模型?

时间:2016-01-11 05:15:17

标签: c# asp.net asp.net-mvc api rest

所以我是ASP / .NET / C#游戏的新手,所以非常感谢你能给我提供的任何帮助。我目前有一个名为" ProjectController.cs"有一个动作"索引"这应该通过一个"项目列表"到了视野。我已经开始创建控制器,动作,API调用和视图。但是,根据提供的屏幕截图 - 我遇到API调用本身的错误 - 我试图将模型列表返回到控制器以传递给视图。我通过从http://10000ft.com api加载JSON响应来完成此操作。在尝试根据模型创建/验证新项目对象时,为什么我不断被卡住了?

ProjectController.cs:

namespace MVCProject1.Controllers
{
    public class ProjectController : Controller
    {
        // GET: Project
        public ActionResult Index()
        {
            List<Models.Project> projects = APICalls.GetAllProjects();
            return View(projects);
        }
    }
}

项目控制人员/索引行动视图:

@{
    ViewBag.Title = "Project Index";
}
@model IEnumerable<MVCProject1.Models.Project>

@foreach (var project in Model)
{
    @project.name <br />
}

ProjectModel.cs:

namespace MVCProject1.Models
{
    public class Project
    {
        public int id { get; set; }
        public bool archived { get; set; }
        public object archived_at { get; set; }
        public string description { get; set; }
        public string guid { get; set; }
        public string name { get; set; }
        public object parent_id { get; set; }
        public object phase_name { get; set; }
        public string project_code { get; set; }
        public string secureurl { get; set; }
        public string secureurl_expiration { get; set; }
        public int settings { get; set; }
        public int timeentry_lockout { get; set; }
        public string ends_at { get; set; }
        public string starts_at { get; set; }
        public object deleted_at { get; set; }
        public string created_at { get; set; }
        public string updated_at { get; set; }
        public bool use_parent_bill_rates { get; set; }
        public string thumbnail { get; set; }
        public string type { get; set; }
        public bool has_pending_updates { get; set; }
        public string client { get; set; }
        public string project_state { get; set; }

    }
}

APICalls.cs:

namespace MVCProject1
{
    public class APICalls
    {
        private const string baseUrl = "https://api.10000ft.com/api/v1/{0}?&auth=temp12345=&{1}";

        #region PROJECTS
        public static List<Models.Project> GetAllProjects()
        {
            string requestUrl = string.Format(baseUrl, "projects", "&per_page=200");

            var resultString = RestClient.makeAPICall(requestUrl);
            var resultObject = JsonConvert.DeserializeObject(resultString);

            List<Models.Project> projects = new List<Models.Project>();

            foreach (var project in resultObject.data)
            {
                projects.Add(project);
            }
            return projects;
        }
        #endregion

    }
}

RESTClient.cs:

namespace MVCProject1
{
    public static class RestClient
    {
        public static string makeAPICall(string requestUrl)
        {
            var syncClient = new WebClient();
            var content = syncClient.DownloadString(requestUrl);
            Trace.WriteLine(content.ToString());
            return content;
        }
    }

}

Image of error I'm receiving

1 个答案:

答案 0 :(得分:0)

我认为您对产品列表的反序列化存在问题。 试试

var projects = JsonConvert.DeserializeObject<List<Models.Project>>(resultString);

而不是

var resultObject = JsonConvert.DeserializeObject(resultString);

List<Models.Project> projects = new List<Models.Project>();

foreach (var project in resultObject.data)
{
    projects.Add(project);
}

并检查projects值是否为空,并且在反序列化时不会发生错误