【问题标题】:how to get json data from external rest full web service in asp.net mvc 4如何从asp.net mvc 4中的外部rest完整Web服务获取json数据
【发布时间】:2017-04-25 13:32:55
【问题描述】:

我正在尝试从外部 rest url(http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3) 获取 json 数据并将其显示在我的 asp.net mvc 应用程序的网页上。为此,我编写了一些代码,即

控制者---

namespace MyMVCApplication.Controllers
{
    public class EmployeeController : Controller
    {       

        string Baseurl = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3";
        public async Task<ActionResult> StateDetails()
        {
            List<State> listStateInfo = new List<State>();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(Baseurl);
                client.DefaultRequestHeaders.Clear();

                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage Res = await client.GetAsync(Baseurl);
                if (Res.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api   
                    var StateResponse = Res.Content.ReadAsStringAsync().Result;

                    //Deserializing the response recieved from web api and storing into the Employee list  
                    listStateInfo = JsonConvert.DeserializeObject<List<State>>(StateResponse);

                }  

                return View(listStateInfo);
            }

        }

    }
}

Model----
namespace MyMVCApplication.Models
{
    public class State
    {
        public int ObjectID { get; set; }
        public string StateName { get; set; }
        public int Black { get; set; }
        public int Population { get; set; }
    }
}

调试代码时出现错误:“Newtonsoft.Json.JsonReaderException:解析值时遇到意外字符:<.path line position at jsonconvert.deserializeobject.>

请建议我如何解决这个问题。

【问题讨论】:

  • 如果您只是将该 url 放入 chrome 中,它会返回一个 html 页面而不是 json。它似乎还希望您登录或传递身份验证令牌。你有这两个吗?你没有在你的代码中传递任何一个。

标签: json asp.net-mvc


【解决方案1】:

将您的网址替换为http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3?f=pjson 以获取Web 服务的json 版本

【讨论】:

    猜你喜欢
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 2015-04-18
    • 1970-01-01
    相关资源
    最近更新 更多