【发布时间】:2017-07-10 10:14:32
【问题描述】:
我知道以前有人问过这个问题,但我找不到解决这个问题的答案。
我正在向返回 json 的 Web 服务发出请求,然后我使用 json.net 将该 json 作为对象保存在列表中。
List<myclass> result;
var request = new RestRequest(url, Method.POST);
//Set the parameters of the request
//[...]
IRestResponse response = client.Execute(request)
Console.WriteLine(response.Content);
//response.Content = [{"nomPrecio":"string","nomPrecioEN":"string","IDrangoPrecio":0,"IDPoblacionMv":0,"NumOfertas":0,"NumOVotaciones":0,"Imagen":"anUrl"}]
//Everything works fine until here, and I can see the json is being received OK, but then...
result = JsonConvert.DeserializeObject<List<myclass>>(response.Content);
然后控制台显示此消息:
Rest 异常:找不到用于类型 mynamespace.myclass 的构造函数。一个类应该有一个默认构造函数、一个带参数的构造函数或一个标有 JsonConstructor 属性的构造函数。路径“[0].nomPrecio”,第 1 行,位置 14。
namespace mynamespace
{
public class myclass
{
public myclass()
{
}
public myclass(string nomPrecio, string nomPrecioEN, int IDrangoPrecio, int IDPoblacionMv, int NumOfertas, int NumOVotaciones, string Imagen)
{
this.nomPrecio = nomPrecio;
this.nomPrecioEN = nomPrecioEN;
this.IDrangoPrecio = IDrangoPrecio;
this.IDPoblacionMv = IDPoblacionMv;
this.NumOfertas = NumOfertas;
this.NumOVotaciones = NumOVotaciones;
this.Imagen = Imagen;
}
public string nomPrecio { get; set; }
public string nomPrecioEN { get; set; }
public int IDrangoPrecio { get; set; }
public int IDPoblacionMv { get; set; }
public int NumOfertas { get; set; }
public int NumOVotaciones { get; set; }
public string Imagen { get; set; }
}
}
更奇怪的是,我对应用程序中的其他类做了同样的事情,没有人返回这个错误,它们都有效。
我尝试了很多诸如“json2csharp”之类的东西,但没有任何效果。
关于我可能做错了什么的任何提示?谢谢
【问题讨论】:
标签: c# xamarin constructor xamarin.ios json.net