0

Can anyone tell me if there is a built-in way to format the output in the following format? I have not run across this before. I can write a formatter but if I don't have to that's a plus.

delete/bulk?apiKey=8ea7c969eff8ab3819b9090d225c8625a0d2668d&listCrmId[0]=30871295&listCrmId1=30871296

My entity is as follows

public class BulkDelete
{
    [JsonProperty(PropertyName = "apiKey")]
    public string ApiKey { get; set; }
    [JsonProperty(PropertyName = "listCrmId")]
    public int[] ListCrmId { get; set; }

}

Posting my solution so it's available for others. No solution I found would output the Array Collection by name[postion]=value so I modified another extension method from here.

    public static string GetQueryStringFromArray(this object obj)
    {
        var result = new List<string>();
        var props = obj.GetType().GetProperties().Where(p => p.GetValue(obj, null) != null);
        foreach (var p in props)
        {
            var value = p.GetValue(obj, null);
            switch (value)
            {
                case ICollection enumerable when p.PropertyType.IsArray:
                    var sd = enumerable.Cast<object>();
                    for (int i = 0; i < sd.Count(); i++)
                    {
                        result.Add($"{p.Name}[{i}]={HttpUtility.UrlEncode(sd.ElementAt(i).ToString())}");
                    }
                    break;
                case ICollection enumerable:
                    result.AddRange(enumerable.Cast<object>().Select(v => $"{p.Name}={HttpUtility.UrlEncode(v.ToString())}"));
                    break;
                default:
                    result.Add($"{p.Name}={HttpUtility.UrlEncode(value.ToString())}");
                    break;
            }
        }

        return string.Join("&", result.ToArray());
    }

Output returns ApiKey=a415ae8151957de117869389aa3e30c068b7eaa5&ListCrmId[0]=123450&ListCrmId1=123451&ListCrmId[2]=123452&ListCrmId[3]=123453&ListCrmId[4]=123454&ListCrmId[5]=123455&ListCrmId[6]=123456&ListCrmId[7]=123457&ListCrmId[8]=123458&ListCrmId[9]=123459&ListCrmId[10]=1234510&ListCrmId[11]=1234511&ListCrmId[12]=1234512&ListCrmId[13]=1234513&ListCrmId[14]=1234514&ListCrmId[15]=1234515&ListCrmId[16]=1234516&ListCrmId[17]=1234517&ListCrmId[18]=1234518&ListCrmId[19]=1234519

Thanks @John

Tim
  • 1,249
  • 5
  • 28
  • 54
  • What you're asking to isn't how to send JSON at all, but how to convert your class properties into _query string_ parameters. – ProgrammingLlama Jun 14 '18 at 02:56
  • @john that's true. I didn't think about that. So would you recommend writing a formatter? or do you have another suggestion – Tim Jun 14 '18 at 02:58
  • 1
    If there's not a NuGet package, I'd look into using something like [this](https://stackoverflow.com/questions/6848296/how-do-i-serialize-an-object-into-query-string-format) as a base and add functionality for handling `IEnumerable`s (List, Array, etc.) to it. – ProgrammingLlama Jun 14 '18 at 03:05
  • @john I found a couple that are a good starting point. They will need some tweaking. Thanks for the pointer – Tim Jun 14 '18 at 03:14

0 Answers0