C# Newtonsoft.Json.JsonSerializationException:<;获取异常详细信息时超时>;发生
我正在使用rest服务,但当我反序列化JSON字符串时,它会引发此异常?这个例外意味着什么 等级C# Newtonsoft.Json.JsonSerializationException:<;获取异常详细信息时超时>;发生,c#,xamarin.forms,observablecollection,jsonconvert,C#,Xamarin.forms,Observablecollection,Jsonconvert,我正在使用rest服务,但当我反序列化JSON字符串时,它会引发此异常?这个例外意味着什么 等级 消费者 这就是我用来使用rest服务,然后它向我发送一个JSON字符串,我将其反序列化为一个称为products的类型为Observable Collection的对象 public ObservableCollection<Product> products = new ObservableCollection<Product>(); public async Task
消费者 这就是我用来使用rest服务,然后它向我发送一个JSON字符串,我将其反序列化为一个称为products的类型为Observable Collection的对象
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;
}
}
异常消息
请注意,拉动单个产品时会这样说,但拉动所有对象时也会发生同样的情况位置会随之改变
转换值“{”PROD:“5510B-BK”,“DPID:”0,“SDID:”0,“CGID:”0,“SCID:”0,“SPID:”0,“PDSC:”5510B黑船鞋“,”PBRN:”鞋类直销“,”SESN:”2018“,”IQTY:”双“,”币“:”ZAR“,”销售“:0.0000,”PSKU:”5510B-BK“,”PSZE:”12“,”PCOL:”BK“,”PPCD:“A”,“DPDS:“无”}输入'WarehouseProMobile.Models.Product'。路径“”,第1行,位置427
解决了的
结果是我的RESTAPI序列化为JSON,当对象通过网络发送时,web服务器也序列化了我的对象,这使得字符串无用。通过将API调整为仅发送对象来修复此问题。这是您的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;
}
}
将您的操作方法修改为:
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;
}
}
公共异步任务GetProducts()
{
尝试
{
字符串uri=url+“/产品;
_client.Timeout=TimeSpan.FromSeconds(300);
HttpRequestMessage=新的HttpRequestMessage(HttpMethod.Get,uri);
var response2=wait_client.SendAsync(消息);
ObservableCollection products=新的ObservableCollection();
if(响应2.IsSuccessStatusCode)
{
var content=await response2.content.ReadAsStringAsync();
Product Product=JsonConvert.DeserializeObject(内容);
产品。添加(产品);
}
退货产品;
}
捕获(例外情况除外)
{
掷骰子;
}
}
这是您的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;
}
}
将您的操作方法修改为:
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;
}
}
公共异步任务GetProducts()
{
尝试
{
字符串uri=url+“/产品;
_client.Timeout=TimeSpan.FromSeconds(300);
HttpRequestMessage=新的HttpRequestMessage(HttpMethod.Get,uri);
var response2=wait_client.SendAsync(消息);
ObservableCollection products=新的ObservableCollection();
if(响应2.IsSuccessStatusCode)
{
var content=await response2.content.ReadAsStringAsync();
Product Product=JsonConvert.DeserializeObject(内容);
产品。添加(产品);
}
退货产品;
}
捕获(例外情况除外)
{
掷骰子;
}
}
这是我通常做的事情: 我创建了一个与JSON属性匹配的类(尽可能使用nullable): 然后,我会简单地写下:
var jsonisedObject = JsonConvert.DeserializeObject<List<Testingo.Obj>>(jsonStringHere);
var jsonisedObject=JsonConvert.DeserializeObject(jsonStringher);
将jsonstringher
更改为包含实际JSON数据的字符串
您可以要求VisualStudio为您生成一个支持JSON的类。只需复制json数据,然后在类内部,单击编辑->粘贴特殊->粘贴json作为类 这是我通常做的: 我创建了一个与JSON属性匹配的类(尽可能使用nullable): 然后,我会简单地写下:
var jsonisedObject = JsonConvert.DeserializeObject<List<Testingo.Obj>>(jsonStringHere);
var jsonisedObject=JsonConvert.DeserializeObject(jsonStringher);
将jsonstringher
更改为包含实际JSON数据的字符串
您可以要求VisualStudio为您生成一个支持JSON的类。只需复制json数据,然后在类内部,单击编辑->粘贴特殊->粘贴json作为类 请添加异常详细信息,如
Message
、StackTrace
和HResult
?uri
是第二个代码块中的未终止字符串如何获取异常消息等?json字符串是字典而不是列表。尝试将prodlist更改为产品实例。请添加异常详细信息,如Message
、StackTrace
和HResult
?uri
是第二个代码块中的未终止字符串如何获取异常消息等?json字符串是字典而不是列表。尝试将您的产品列表更改为产品实例。我不明白,Product
和products
声明在哪里?@iakobski,修改了我的回答products
来自OP的帖子,我不知道我不明白,Product
和products
声明在哪里?@iakobski,修改我的答案产品
来自OP的帖子,我不知道