将c#字符串数组转换为json的数组

Mat*_*ner 0 c# json.net

我有一个字符串数组.

images[0] = 1255nr_171229_620_003_0040.jpg
images[1] = 1255nr_171229_620_003_0061.jpg
images[2] = 1255nr_171229_620_003_0431.jpg
images[3] = 1255nr_171229_620_003_0467.jpg
Run Code Online (Sandbox Code Playgroud)

我需要按API期望序列化它们:

"favorites":["1255nr_171229_620_003_0040.jpg", "1255nr_171229_620_003_0061.jpg", "1255nr_171229_620_003_0431.jpg", ...]
Run Code Online (Sandbox Code Playgroud)

这就是我现在所拥有的:

using Newtonsoft.Json;

HttpClient client = new HttpClient();
client.BaseAddress = new Uri(postURL);
client.DefaultRequestHeaders.Add("Authorization", token);
string POSTcall = string.Format("{{\"name\": \"{0}\",\"email\": \"{1}\",\"phone\": \"{2}\",\"favorites\": \"{9}\"}}", CustomerName, Email, Phone, images[]);

StringContent stringContent = new StringContent(POSTcall, UnicodeEncoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(new Uri(postURL), stringContent);
Run Code Online (Sandbox Code Playgroud)

我看的每个例子都只是一个键值对,但我不知道如何为一个键做一个值数组.

IT *_*Han 5

尝试SerializeObjectAnonymousType

void Main()
{
    var CustomerName = "xxx";
    var Email = "xxxx@xxxx";
    var Phone = "88690xxxxxxx";
    var images = new string[]{"1255nr_171229_620_003_0040.jpg","1255nr_171229_620_003_0061.jpg","1255nr_171229_620_003_0431.jpg"};
    string POSTcall= JsonConvert.SerializeObject(new {CustomerName,Email,Phone,favorites=images});
    /*
    result :
        {
            "CustomerName":"xxx","Email":"xxxx@xxxx","Phone":"88690xxxxxxx"
            ,"favorites":["1255nr_171229_620_003_0040.jpg","1255nr_171229_620_003_0061.jpg","1255nr_171229_620_003_0431.jpg"]
        }   
    */
    StringContent stringContent = new StringContent(POSTcall, UnicodeEncoding.UTF8, "application/json");
    //......
}
Run Code Online (Sandbox Code Playgroud)