简体   繁体   English

消耗.net中的Rest API时出错

[英]Error consuming rest API in .net

I'm trying to consume a rest API but I'm getting the next error: 我正在尝试使用rest API,但遇到下一个错误:

Exception Details: Newtonsoft.Json.JsonException: Unexpected JSON token while reading DataTable: StartObject 异常详细信息:Newtonsoft.Json.JsonException:读取DataTable:StartObject时出现意外的JSON令牌

also, if changing endpoint I get: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown 另外,如果更改端点,我会得到:System.OutOfMemoryException:引发了类型为'System.OutOfMemoryException'的异常

which is pointing to this line of code: 指向以下代码行:

var table = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Data.DataTable>(data);

not really sure how to fix it? 不太确定如何解决? Anyone can spot what the error is or how to fix it?... 任何人都可以发现错误或解决方法?

thanks.. 谢谢..

My controller is: 我的控制器是:

public async System.Threading.Tasks.Task<ActionResult> About()
    {
        string url = "https://restcountries.eu/rest/v1/region/africa";

        using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
        {
            client.BaseAddress = new Uri(url);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            System.Net.Http.HttpResponseMessage response = await client.GetAsync(url);
            if (response.IsSuccessStatusCode)
            {
                var data = await response.Content.ReadAsStringAsync();
                var table = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Data.DataTable>(data);

                System.Web.UI.WebControls.GridView gView = new System.Web.UI.WebControls.GridView();
                gView.DataSource = table;
                gView.DataBind();
                using (System.IO.StringWriter sw = new System.IO.StringWriter())
                {
                    using (System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw))
                    {
                        gView.RenderControl(htw);
                        ViewBag.ReturnedData = sw.ToString();
                    }
                }
            }
        }
        return View();

You shouldn't be deserializing into a DataTable . 您不应该反序列化到DataTable A DataTable is a generic, in-memory representation of some relational data. DataTable是某些关系数据的通用内存表示形式。 With the following deserialization lines of code you have: 使用以下反序列化代码行,您可以:

var data = await response.Content.ReadAsStringAsync();
var table = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Data.DataTable>(data);

you are asking your .NET code to go grab some data from the internet and turn that string of data into an in-memory representation of relational data, when, at this point, it's really only a string of characters. 您正在要求.NET代码从Internet上获取一些数据,然后将该字符串转换为关系数据的内存表示形式,此时,它实际上只是一个字符串。

You should create a model of a country with the properties that are returned from the API you're querying. 您应该使用要查询的API返回的属性创建国家/地区模型。

You can then deserialize that data and play with it in .NET by doing something like the following: 然后,您可以通过执行以下操作来反序列化该数据并在.NET中进行处理:

Newtonsoft.Json.JsonConvert.DeserializeObject<IList<MyCountryModel>>(data);

After reading the comments I got to solve the model problem and defined it like this,....thanks 看完注释后,我就解决了模型问题并像这样定义它,....谢谢

public class Log2
{
    public string error { get; set; }
    public Log3[] Logs { get; set; }
}

 public class Log3
{
    public string ProdLoggerIdx { get; set; }
    public string OfficeID { get; set; }
    public string Agent { get; set; }
    public string CommandSent { get; set; }
    public string HostResponse { get; set; }
    public string CommandSentDate { get; set; }
    public string CommandReceivedDate { get; set; }

}

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

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