Newtonsoft.Json.JsonSerializationException:发生<timeout超出=“ =” getting =“” exception =“” details =“”>

时间:2019-04-09 06:05:52

标签: c# xamarin.forms observablecollection jsonconvert

我正在使用rest服务,但是当我取消序列化JSON字符串时会引发此异常吗?这个例外是什么意思?

课程

public class Product
{
    public string PROD
    {
        get { return prod; }
        set { prod = value; }
    }

    //Department Number
    [JsonProperty("DPID")]
    public int DPID
    {
        get { return dpid; }
        set { dpid = value; }
    }

    //Sub Department Number
    [JsonProperty("SDID")]
    public int SDID
    {
        get { return sdid; }
        set { sdid = value; }
    }

    //Category Number
    [JsonProperty("CGID")]
    public int CGID
    {
        get { return cgid; }
        set { cgid = value; }
    }

    //Sub Category Number
    [JsonProperty("SCID")]
    public int SCID
    {
        get { return scid; }
        set { scid = value; }
    }

    //Product Description
    [JsonProperty("PDSC")]
    public string PDSC
    {
        get { return pdsc; }
        set { pdsc = value; }
    }

    //Product Brand
    [JsonProperty("PBRN")]
    public string PBRN
    {
        get { return pbrn; }
        set { pbrn = value; }
    }

    //Season Code
    [JsonProperty("SESN")]
    public string SESN
    {
        get { return sesn; }
        set { sesn = value; }
    }

    //Issue Quantity
    [JsonProperty("IQTY")]
    public string IQTY
    {
        get { return iqty; }
        set { iqty = value; }
    }

    //Currency Code
    [JsonProperty("CURR")]
    public string CURR
    {
        get { return curr; }
        set { curr = value; }
    }

    //Selling Price
    [JsonProperty("SELL")]
    public decimal SELL
    {
        get { return sell; }
        set { sell = value; }
    }

    //Product SKU Code
    [JsonProperty("PSKU")]
    public string PSKU
    {
        get { return psku; }
        set { psku = value; }
    }

    //Product Size
    [JsonProperty("PSZE")]
    public string PSZE
    {
        get { return psze; }
        set { psze = value; }
    }

    //Product Colour
    [JsonProperty("PCOL")]
    public string PCOL
    {
        get { return pcol; }
        set { pcol = value; }
    }

    //Pre-pack Code
    [JsonProperty("PPCD")]
    public string PPCD
    {
        get { return ppcd; }
        set { ppcd = value; }
    }

    //Image URL
    public string IURL
    {
        get { return iurl; }
        set { iurl = value; }
    }        

    [JsonProperty("DPDS")]
    public string DPDS
    {
        get { return dpds; }
        set { dpds = value; }
    }
}

消费者

这是我用来消耗其余服务的方式,然后它向我发送一个JSON字符串,然后将其反序列化为Observable Collection类型的对象,称为product。

public ObservableCollection<Product> products = new ObservableCollection<Product>();

public async Task<ObservableCollection<Product>> GetProducts()
{
    try
    {
        string uri = url + "/product;
        _client.Timeout = TimeSpan.FromSeconds(300);

        HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, uri);
        var response2 = await _client.SendAsync(message);

        if (response2.IsSuccessStatusCode)
        {
            var content = await response2.Content.ReadAsStringAsync();
            var prodlist = JsonConvert.DeserializeObject<ObservableCollection<Product>>(content);
            products = prodlist;
            return products;
        }
        else if (response2.StatusCode == HttpStatusCode.NotFound)
        {
            return products;
        }

        return products;
    }
    catch (JsonException ex)
    {
        throw ex;
    }
}

JSON字符串

URI在被调用时会返回这种格式的大字符串。

{
  "PROD": "5510B-BK        ",
  "DPID": 0,
  "SDID": 0,
  "CGID": 0,
  "SCID": 0,
  "SPID": 0,
  "PDSC": "5510B BLACK BOAT SHOE                             ",
  "PBRN": "Footwear Direct",
  "SESN": "2018  ",
  "IQTY": "Pair           ",
  "CURR": "ZAR",
  "SELL": 0,
  "PSKU": "5510B-BK            ",
  "PSZE": "12      ",
  "PCOL": "BK                                 ",
  "PPCD": "A     ",
  "DPDS": "None                     "
}

The exception I get

异常消息

请注意,这就是拉单个产品时所说的,但是拉所有对象时发生的事情都是一样的。

转换值“ {” PROD“:” 5510B-BK“,” DPID“:0,” SDID“:0,” CGID“:0,” SCID“:0,” SPID“:0,” PDSC “:” 5510B BLACK BOAT SHOE“,” PBRN“:”鞋类直销“,” SESN“:” 2018“,” IQTY“:”对“,” CURR“:” ZAR“,”卖出“:0.0000,” PSKU “:” 5510B-BK“,” PSZE“:” 12“,” PCOL“:” BK“,” PPCD“:” A“,” DPDS“:”无“}”键入'WarehouseProMobile.Models.Product' 。路径”,第1行,位置427。

已解决

证明我的其余API序列化为JSON,并且当对象通过网络发送时,Web服务器也序列化了我的对象,这使字符串无用。通过调整API以仅发送对象来解决此问题。

2 个答案:

答案 0 :(得分:0)

这是您的POCO:

public class Product
{
    public string PROD { get; set; }

    //Department Number
    [JsonProperty("DPID")]
    public int DPID { get; set; }


    //Sub Department Number
    [JsonProperty("SDID")]
    public int SDID { get; set; }

    //Category Number
    [JsonProperty("CGID")]
    public int CGID { get; set; }

    //Sub Category Number
    [JsonProperty("SCID")]
    public int SCID { get; set; }

    //Product Description
    [JsonProperty("PDSC")]
    public string PDSC { get; set; }


    //Product Brand
    [JsonProperty("PBRN")]
    public string PBRN { get; set; }


    //Season Code
    [JsonProperty("SESN")]
    public string SESN { get; set; }

    //Issue Quantity
    [JsonProperty("IQTY")]
    public string IQTY { get; set; }


    //Currency Code
    [JsonProperty("CURR")]
    public string CURR { get; set; }

    //Selling Price
    [JsonProperty("SELL")]
    public decimal SELL { get; set; }

    //Product SKU Code
    [JsonProperty("PSKU")]
    public string PSKU { get; set; }

    //Product Size
    [JsonProperty("PSZE")]
    public string PSZE { get; set; }

    //Product Colour
    [JsonProperty("PCOL")]
    public string PCOL { get; set; }

    //Pre-pack Code
    [JsonProperty("PPCD")]
    public string PPCD { get; set; }
    //Image URL
    public string IURL { get; set; }

    [JsonProperty("DPDS")]
    public string DPDS { get; set; }
}

将您的操作方法修改为:

public async Task<ObservableCollection<Product>> GetProducts()
{
    try
    {
        string uri = url + "/product;
        _client.Timeout = TimeSpan.FromSeconds(300);

        HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, uri);
        var response2 = await _client.SendAsync(message);

        ObservableCollection<Product> products = new ObservableCollection<Product>();
        if (response2.IsSuccessStatusCode)
        {
            var content = await response2.Content.ReadAsStringAsync();
            Product product = JsonConvert.DeserializeObject<Product>(content);
            products.Add(product);
        }

        return products;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

答案 1 :(得分:0)

这是我通常要做的:

我创建一个与JSON attrs匹配的类(尝试在可能的地方使用可空值):

public class Testingo
{

    public class Obj
    {
        public string PROD { get; set; }
        public int? DPID { get; set; }
        public int? SDID { get; set; }
        public int? CGID { get; set; }
        public int? SCID { get; set; }
        public int? SPID { get; set; }
        public string PDSC { get; set; }
        public string PBRN { get; set; }
        public string SESN { get; set; }
        public string IQTY { get; set; }
        public string CURR { get; set; }
        public int? SELL { get; set; }
        public string PSKU { get; set; }
        public string PSZE { get; set; }
        public string PCOL { get; set; }
        public string PPCD { get; set; }
        public string DPDS { get; set; }
    }

}

然后,我会简单地写:

var jsonisedObject = JsonConvert.DeserializeObject<List<Testingo.Obj>>(jsonStringHere);

jsonStringHere更改为包含实际JSON数据的字符串

您可以要求Visual Studio为您生成一个JSON就绪类。只需复制json数据,然后在一个类中,单击Edit-> Paste Special-> Paste Json作为类!