【发布时间】:2013-01-16 02:29:51
【问题描述】:
知道为什么我会收到序列化错误吗?我用其他网络方法尝试过,它奏效了。仅此而已。
public T Execute<T>(RestRequest request) where T : new()
{
var client = new RestClient
{BaseUrl = BaseUrl, Authenticator = new HttpBasicAuthenticator(AccountSid, SecretKey)};
var response = client.Execute<T>(request);
if (response.ErrorException != null)
{
throw response.ErrorException;
}
return response.Data;
}
这是对象。
public class Order
{
public Order() { }
public string ProductName { get; set; }
public double SoldPrice { get; set; }
public double Fees { get; set; }
public String BuyerEmail { get; set; }
public String BuyerName { get; set; }
}
这是我的 JSON。
"[{\"ProductName\":\"Demo Hinges\",\"SoldPrice\":700.0,\"Fees\":21.0,\"Size\":\"\",\"BuyerEmail\":\"\",\"BuyerName\":\"\"}]"
我收到此错误。
System.InvalidCastException:无法将“RestSharp.JsonArray”类型的对象转换为“System.Collections.Generic.IDictionary`2[System.String,System.Object]”类型。 在 RestSharp.Deserializers.JsonDeserializer.FindRoot(字符串内容) 在 RestSharp.Deserializers.JsonDeserializer.Deserialize[T](IRestResponse 响应) 在 RestSharp.RestClient.Deserialize[T](IRestRequest request, IRestResponse raw)}
【问题讨论】:
-
json 是错误中提到的 JsonArray 类型,您正在尝试将其转换为字典或对象更改反序列化以接受数组而不是对象。