Webservice - 无法序列化,因为它没有无参数构造函数

时间:2014-05-23 18:43:11

标签: c# asp.net web-services asmx

我从web服务调用以下方法 - 我不断收到错误消息,因为它没有无参数构造函数而无法序列化。"

  [WebMethod]
    public ArrayList GetPayers()
    {
        string PROVIDER_JSON = "";

        ArrayList list = new ArrayList();
        using (var webClient = new System.Net.WebClient())
        {
            PROVIDER_JSON = webClient.DownloadString("https://www.eligibleapi.com/resources/information-sources.json");

            List<EligibleProviderType2> UserList = JsonConvert.DeserializeObject<List<EligibleProviderType2>>(PROVIDER_JSON);

            for (int i = 0; i < UserList.Count; i++)
            {
                foreach (string inventoryItem in UserList[i].PayerName)
                {
                    list.Add(new
                    {
                        PayerID = UserList[i].PayerID,
                        PayerName = inventoryItem
                    });
                }
            }

            return list;
        }
    }

哪个叫这个类 - 我认为它有一个无参数构造函数?

[JsonObject(MemberSerialization.OptIn)]
public class EligibleProviderType2
{
    EligibleProviderType2(){}

    [JsonProperty(PropertyName = "payer_id")]
    public string PayerID { get; set; }

    [JsonProperty(PropertyName = "names")]
    public IList<string> PayerName { get; set; }

} // EligibleProvider

2 个答案:

答案 0 :(得分:2)

让你的构造函数公开。序列化代码无法看到它。

答案 1 :(得分:2)

您的EligibleProviderType2的构造函数似乎是private。 Private是类字段和方法的默认值。删除它(因为编译器将为您创建一个)或更改如下:

[JsonObject(MemberSerialization.OptIn)]
public class EligibleProviderType2
{
    public EligibleProviderType2(){}

    [JsonProperty(PropertyName = "payer_id")]
    public string PayerID { get; set; }

    [JsonProperty(PropertyName = "names")]
    public IList<string> PayerName { get; set; }
} // EligibleProvider