C# 在 class 中重复相同的字段名称以允许序列化到 JSON

C# repeating the same field name in class to allow serializing to JSON

我正在尝试构建一个 class,我将其序列化为网络请求的主体。

序列化对象时我试图实现的输出是:

{
  "findCompletedItemsRequest": {
    "keywords": "searchtext",
    "itemFilter": [
      {
        "name": "SoldItemsOnly",
        "value": "true"
      }
    ],
    "outputSelector": "PictureURLLarge",
    "outputSelector": "SellerInfo",
    "paginationInput": {
      "entriesPerPage": "100",
      "pageNumber": "1"
    }
  }
}

对于字段 'outputSelector',我可以将它放入其中 0 次、1 次或多次。 我将如何在我的 class 中定义它?我试过搜索,但我不确定我想要实现的目标可能被称为。如果我使用列表或数组,它只会创建其中有一个数组的 outputSelect JSON 属性,而不是多个 outputSelector 属性。

这是我目前的情况(欢迎其他批评,因为我来自过程编程背景)

public class eBaySearchBody
        {
            public FindCompletedItemsRequest findCompletedItemsRequest = new FindCompletedItemsRequest();
            public class FindCompletedItemsRequest
            {
                public string keywords { get; set; }
                public List<itemFilters> itemFilter { get; set; }
                public string sortOrder { get; set; }
                public PaginationInput paginationInput = new PaginationInput();

            }
            public class PaginationInput
            {
                public string entriesPerPage { get; set; }
                public string pageNumber { get; set; }
            }
            public class itemFilters
            {
                public string name { get; set; }
                public string value { get; set; }
            }
        }

这不是有效的 JSON。它有 SyntaxError: Duplicate key 'outputSelector' on line 9.

如果您考虑使用 outputSelector 数组,则需要如下修改代码以生成有效的 JSON。 样本有效 JSON:

{
    "findCompletedItemsRequest": {
        "keywords": "searchtext",
        "itemFilter": [{
            "name": "SoldItemsOnly",
            "value": "true"
        }],
        "outputSelector": ["PictureURLLarge", "SellerInfo"],
        "paginationInput": {
            "entriesPerPage": "100",
            "pageNumber": "1"
        }
    }
}

示例 C# 代码修改:

namespace Solutions{
    using System.Collections.Generic;
    using Newtonsoft.Json;
    public class EBaySearchBody{
        public class FindCompletedItemsRequest{
            [JsonProperty("keywords", Order = 1)]
            public string Keywords { get; set; }

            [JsonProperty("itemFilter",Order = 2)]
            public List<ItemFilter> ItemFilters { get; set; }

            [JsonProperty("outputSelector", Order = 3)]
            public List<string> OutputSelectors { get; set; }

            [JsonProperty("paginationInput", Order = 4)]
            public PaginationInput PaginationInput { get; set; }
        }
        public class PaginationInput{
            [JsonProperty("entriesPerPage", Order = 1)]
            public string EntriesPerPage { get; set; }

            [JsonProperty("pageNumber", Order = 2)]
            public string PageNumber { get; set; }
        }
        public class ItemFilter{
            [JsonProperty("name", Order = 1)]
            public string Name { get; set; }

            [JsonProperty("value", Order = 2)]
            public string Value { get; set; }
        }
        [JsonProperty("findCompletedItemsRequest")]
        public FindCompletedItemsRequest FindCompletedItemsRequestObject { get; set; }

        /// <summary>
        /// Create a <see cref="EBaySearchBody"/>object and serialize it to a JSON stream
        /// </summary>
        /// <returns></returns>
        public static string WriteFromObject()
        {
            //Create EbaySearchBody object.  
            EBaySearchBody searchBody = new EBaySearchBody(){
                    FindCompletedItemsRequestObject = new FindCompletedItemsRequest(){
                        Keywords = "searchtext",
                        ItemFilters = new List<ItemFilter>(){
                            new ItemFilter {
                                Name = "SoldItemsOnly",
                                Value = "true"
                            }
                        },
                        OutputSelectors = new List<string>(){
                            "PictureURLLarge",
                            "SellerInfo"
                        },
                        PaginationInput = new PaginationInput(){
                            EntriesPerPage = "100",
                            PageNumber = "1"
                        }
                    }
                };
            return JsonConvert.SerializeObject(searchBody);
        }
    }
}