“对象”不包含“ DeserializeObject”的定义,也没有扩展方法“ DeserializeObject”接受“ object”类型的第一个参数

时间:2018-08-17 14:27:30

标签: c# serialization json.net

我试图在下面运行控制台客户端,但出现错误:

  

'object'不包含'DeserializeObject'的定义,也找不到找不到接受'object'类型的第一个参数的扩展方法'DeserializeObject'(您是否缺少using指令或程序集引用?)

class GetQuerys
{
    const string APIKey = "b7ffe33968bf43e892ba62e3ff759e12";

    public static object JsonConvert { get; private set; }

    public static TescoGroceryRoot MakeGroceryRequest(string query, int offset = 0, string limit = "10")
    {
        HttpClient client = new HttpClient();
        var uri = String.Format("https://dev.tescolabs.com/grocery/products/?query={0}&offset={1}&limit={2}", query, offset, limit);

        client.BaseAddress = new Uri(uri);
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
        // Request headers
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", APIKey);

        HttpResponseMessage response = client.GetAsync(uri).Result;

        if (response.IsSuccessStatusCode)
        {
            return JsonConvert.DeserializeObject<TescoGroceryRoot>(response.Content.ReadAsStringAsync().Result);
           // var result = await response.Content.ReadAsAsync<List<TescoGroceryRoot>>();
        }

        return null;
    }

    public static TescoProductRoot GetProductInfo(string gtin = "", string tpnb = "", string tpnc = "")
    {
        HttpClient client = new HttpClient();

        var uri = "https://dev.tescolabs.com/product/?";

        if (!string.IsNullOrEmpty(gtin))
        {
            uri += "gtin=" + gtin;
        }
        if (!string.IsNullOrEmpty(tpnb))
        {
            uri += "tpnb=" + tpnb;
        }
        if (!string.IsNullOrEmpty(tpnc))
        {
            uri += "tpnc=" + tpnc;
        }


        client.BaseAddress = new Uri(uri);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        // Request headers
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", APIKey);

        HttpResponseMessage response = client.GetAsync(uri).Result;


            if (response.IsSuccessStatusCode)
        {

            return JsonConvert.DeserializeObject<TescoProductRoot>(response.Content.ReadAsStringAsync().Result);


        }

        return new TescoProductRoot();
    }


    public class TescoProductRoot
    {
        public List<Product> products { get; set; }
    }
    public class TescoGroceryRoot
    {
        public string Irl { get; set; }
    }
}
}

我还有以下产品类别:

class Product
{
    [Key]
    public int SKU { get; set; }// primary key
    [Required(ErrorMessage = "Not Null")]

    public string ProductName { get; set; }

    public int CategoryID { get; set; }
    //[ForeignKey("Category")]
}

我正在使用此控制台客户端应用程序从Tesco Labs API获取信息。但是由于某种原因,我遇到了这个反序列化错误。

我怎么能

0 个答案:

没有答案