在C#中序列化和反序列化json值以列表[关闭]

5jvtdoz2  于 5个月前  发布在  C#
关注(0)|答案(1)|浏览(62)

已关闭。此问题需要details or clarity。目前不接受回答。
**要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

25天前关闭
Improve this question
因此,对于这项工作,我需要将JSON中的对象放入一个列表中,对象是仅包含姓名,电子邮件和电话号码的客户。
我试了几次,但失败了,如果有人能告诉我或解释我将真的很棒。

namespace customers_manager
{
    internal class Program
    {
        public class Customer
        { 
            public string name { get; set; }          
            public string email { get; set; }
            public int number { get; set; }
        }

        static List<Customer> Datac = new List<Customer>();

        Customer customer = new Customer();
        Console.WriteLine("New Customer data:");
        Console.WriteLine("Name: ");
        cliente.name = Console.ReadLine();
        Console.WriteLine("Email: ");
        cliente.email = Console.ReadLine();
        Console.WriteLine("Number: ");
        cliente.number = Console.ReadLine();
        Datac.Add(customer);
        Console.WriteLine("Finish, Press enter to leave");
        Console.ReadLine();

    }
}

字符串
在此之前,当我尝试序列化时,我只发现错误

6l7fqoea

6l7fqoea1#

您可以使用Newtonsoft.JSON库来序列化和重新序列化JSON。安装NuGet包。
使用示例:

using Newtonsoft.Json; // You'll need this to bring the namespace in scope

List<Cliente> Dados = JsonConvert.DeserializeObject<List<Cliente>>(response);

字符串
其中response是一个包含JSON序列化对象列表的字符串。
要将列表序列化为JSON字符串:

string serializedObject = JsonConvert.SerializeObject(Dados);

相关问题