简体   繁体   English

无法将 JSON 列表反序列化为 C# 中的对象列表

[英]Unable Deserialize the JSON List into List of Objects in C#

I am currently working on a solution that Deserializes the list of email from json array, Once I get the list of email objects, I am validating using another class then set into another list. I am currently working on a solution that Deserializes the list of email from json array, Once I get the list of email objects, I am validating using another class then set into another list.

Below is the code for Deserialize:下面是反序列化的代码:

static void Main(string[] args)
{
    string jsonTest = "{\"Emails\":[\"testUser@gmail.com\",\"testUser2@gmail.com\"]}";

    Class1 contact = JsonConvert.DeserializeObject<Class1>(jsonTest);

    Console.WriteLine(contact.Emails[0]);
    Console.WriteLine(contact.Emails[1]);
}

And, below is the class definition for Class1 for deserialize:而且,下面是用于反序列化的Class1的 class 定义:

public class Class1
{
    private readonly List<ValidateEmail> emailsobj;
    public List<string> Emails
    {
        get
        {
            return emailsobj.Select(o => o.EmailAdd).ToList();
        }
        set
        {
            foreach (string email in value)
            {
                emailsobj.Add(new ValidateEmail(email));
            }
        }
    }
}

And, below is the validate class:并且,下面是validate class:

public class ValidateEmail
{
    public string EmailAdd { get; set; }
    private bool Valid;

    public ValidateEmail(string value)
    {           
        try
        {
            MailAddress mail = new MailAddress(EmailAdd);
            EmailAdd = value;
            Valid = true;
        }
        catch (FormatException)
        {
            Valid = false;
        }
    }
}

Whenever I deserialize I am getting exception on line:每当我反序列化时,我都会在线收到异常:

Console.WriteLine(contact.Emails[0]);

Newtonsoft.Json.JsonSerializationException: Error getting value from 'Emails' on 'JsonGenerator.Class1'. Newtonsoft.Json.JsonSerializationException:从“JsonGenerator.Class1”上的“电子邮件”获取值时出错。 ---> System.ArgumentNullException: Value cannot be null. ---> System.ArgumentNullException:值不能是 null。 (Parameter 'source') (参数“来源”)

It looks like the List<Email> never set.看起来List<Email>从未设置。 Any help on this, it would be much appreciated.对此有任何帮助,将不胜感激。

You have to set the below setting(Replace) if you are using Lists - ObjectCreationHandling .如果您使用 Lists - ObjectCreationHandling ,则必须设置以下设置(替换)。

Replace Always create new objects and enables the setter property for the Lists ! Replace总是创建新对象并为Lists启用 setter 属性!

So after you initialize the new list in Class1 like below:因此,在Class1中初始化新列表后,如下所示:

private readonly List<ValidateEmail> emailsobj = new List<ValidateEmail>();

Change the code to have setting:更改代码以进行设置:

 var settings = new JsonSerializerSettings
      {                
         ObjectCreationHandling = ObjectCreationHandling.Replace           
      };

 Class1 contact = JsonConvert.DeserializeObject<Class1>(jsonTest, settings);

See the detailed explanation here !这里的详细解释!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM