Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用asp.net在C#中创建JSON数组_C#_Json - Fatal编程技术网

使用asp.net在C#中创建JSON数组

使用asp.net在C#中创建JSON数组,c#,json,C#,Json,如何在C#中创建JSON数组,以便插入变量(电子邮件数组),例如“test@gmail;test1@gmail...“你喜欢它吗 [ { "To":[ { "EntryType":2, "username":"jack", "email":"test@gmail.com" } ], "Cc":[

如何在C#中创建JSON数组,以便插入变量(电子邮件数组),例如“test@gmail;test1@gmail...“你喜欢它吗

[
    {
        "To":[
            {
                "EntryType":2,
                "username":"jack",
                "email":"test@gmail.com"
            }
        ],
        "Cc":[
            {
                "EntryType":2,
                "username":"mikle",
                "email":"test1@gmail.com"
            },
            {
                "EntryType":2,
                "username":"jone",
                "email":"test2@gmail.com"
            }
        ],
        "Bcc":[
            {
                "EntryType":2,
                "username":"luis",
                "email":"test3@gmail.com"
            }
        ]
    }
]
使用它,你将永远不会回头


我使用json2csharp.com从JSON生成C类。其结果是:

public class Recepient
{
    public int EntryType { get; set; }
    public string username { get; set; }
    public string email { get; set; }
}

public class Mail
{
    public List<Recepient> To { get; set; }
    public List<Recepient> Cc { get; set; }
    public List<Recepient> Bcc { get; set; }
}

你可能需要重新措辞一下。我不清楚他们在问什么。
using Newtonsoft.Json;

public string Demo()
{
    var mails = new List<Mail>();
    var mail = new Mail();
    mail.To = new List<Recepient>{
        new Recepient
        {
            EntryType = 2, 
            username = "jack", 
            email = "test@gmail.com"
        }
    };

    mail.Cc = new List<Recepient>();
    mail.Bcc = new List<Recepient>();

    mails.Add(mail);
    return JsonConvert.SerializeObject(mails);
}