【问题标题】:How can I send an array as a parameter of a query string in c#?如何在 C# 中将数组作为查询字符串的参数发送?
【发布时间】:2011-01-20 18:32:44
【问题描述】:

我正在尝试使用自制的 Web API 来检索一些数据。文档全部用 PHP 编写。我正在查看的示例是这样的:

$params = array(
    'id' => 1
    ,'data' => array(
        ,'email' => 'example@hasoffers.com'
    )

$url = "www.someapi.com/api?" . http_build_query( $params );

我正在使用 C# WebClient 类,但我不知道如何序列化数据参数:

WebClient wc = new WebClient();
wc.QueryString["id"] = "1";
wc.QueryString["data"] = // I have no idea.

string json = wc.DownloadString(apiUrl);

我尝试了一些变体:

wc.QueryString["data"] = "email=test@stackoverflow.com";
wc.QueryString["data"] = Uri.EscapeDataString("data[email]=test@stackoverflow.com");
wc.QueryString["data"] = Uri.EscapeDataString("email[0]=test@stackoverflow.com");
wc.QueryString["data"] = Uri.EscapeDataString("email=test@stackoverflow.com");

当然,我没有任何 PHP 设置来查看 http_build_query() 实际返回的内容。

【问题讨论】:

    标签: c# php arrays query-string


    【解决方案1】:

    我终于明白了。我想发布这个问题重新启动了我的大脑。

    这是有效的:

    wc.QueryString["data[email]"] = "test@stackoverflow.com";
    

    【讨论】:

    • 知道如何为数组数组动态解决这个问题吗?是否有任何 C# 等价于这个 PHP 混乱?
    • @vojta 数组数组是什么意思?您希望密钥是什么?
    【解决方案2】:

    注意,this question 与这个答案非常相关,所以我也把这个类设为an answer there。该答案将收到此类的任何更新。

    据我所知,没有任何内置功能可以做到这一点。但是,您可以创建自己的类。

    所以我做到了:

    /// <summary>
    ///  Helps up build a query string by converting an object into a set of named-values and making a
    ///  query string out of it.
    /// </summary>
    public class QueryStringBuilder
    {
      private readonly List<KeyValuePair<string, object>> _keyValuePairs
        = new List<KeyValuePair<string, object>>();
    
      /// <summary> Builds the query string from the given instance. </summary>
      public static string BuildQueryString(object queryData, string argSeperator = "&")
      {
        var encoder = new QueryStringBuilder();
        encoder.AddEntry(null, queryData, allowObjects: true);
    
        return encoder.GetUriString(argSeperator);
      }
    
      /// <summary>
      ///  Convert the key-value pairs that we've collected into an actual query string.
      /// </summary>
      private string GetUriString(string argSeperator)
      {
        return String.Join(argSeperator,
                           _keyValuePairs.Select(kvp =>
                                                 {
                                                   var key = Uri.EscapeDataString(kvp.Key);
                                                   var value = Uri.EscapeDataString(kvp.Value.ToString());
                                                   return $"{key}={value}";
                                                 }));
      }
    
      /// <summary> Adds a single entry to the collection. </summary>
      /// <param name="prefix"> The prefix to use when generating the key of the entry. Can be null. </param>
      /// <param name="instance"> The instance to add.
      ///  
      ///  - If the instance is a dictionary, the entries determine the key and values.
      ///  - If the instance is a collection, the keys will be the index of the entries, and the value
      ///  will be each item in the collection.
      ///  - If allowObjects is true, then the object's properties' names will be the keys, and the
      ///  values of the properties will be the values.
      ///  - Otherwise the instance is added with the given prefix to the collection of items. </param>
      /// <param name="allowObjects"> true to add the properties of the given instance (if the object is
      ///  not a collection or dictionary), false to add the object as a key-value pair. </param>
      private void AddEntry(string prefix, object instance, bool allowObjects)
      {
        var dictionary = instance as IDictionary;
        var collection = instance as ICollection;
    
        if (dictionary != null)
        {
          Add(prefix, GetDictionaryAdapter(dictionary));
        }
        else if (collection != null)
        {
          Add(prefix, GetArrayAdapter(collection));
        }
        else if (allowObjects)
        {
          Add(prefix, GetObjectAdapter(instance));
        }
        else
        {
          _keyValuePairs.Add(new KeyValuePair<string, object>(prefix, instance));
        }
      }
    
      /// <summary> Adds the given collection of entries. </summary>
      private void Add(string prefix, IEnumerable<Entry> datas)
      {
        foreach (var item in datas)
        {
          var newPrefix = String.IsNullOrEmpty(prefix)
            ? item.Key
            : $"{prefix}[{item.Key}]";
    
          AddEntry(newPrefix, item.Value, allowObjects: false);
        }
      }
    
      private struct Entry
      {
        public string Key;
        public object Value;
      }
    
      /// <summary>
      ///  Returns a collection of entries that represent the properties on the object.
      /// </summary>
      private IEnumerable<Entry> GetObjectAdapter(object data)
      {
        var properties = data.GetType().GetProperties();
    
        foreach (var property in properties)
        {
          yield return new Entry()
                       {
                         Key = property.Name,
                         Value = property.GetValue(data)
                       };
        }
      }
    
      /// <summary>
      ///  Returns a collection of entries that represent items in the collection.
      /// </summary>
      private IEnumerable<Entry> GetArrayAdapter(ICollection collection)
      {
        int i = 0;
        foreach (var item in collection)
        {
          yield return new Entry()
                       {
                         Key = i.ToString(),
                         Value = item,
                       };
          i++;
        }
      }
    
      /// <summary>
      ///  Returns a collection of entries that represent items in the dictionary.
      /// </summary>
      private IEnumerable<Entry> GetDictionaryAdapter(IDictionary collection)
      {
        foreach (DictionaryEntry item in collection)
        {
          yield return new Entry()
                       {
                         Key = item.Key.ToString(),
                         Value = item.Value,
                       };
        }
      }
    }
    

    像这样:

    // Age=19&Name=John%26Doe&Values%5B0%5D=1&Values%5B1%5D=2&Values%5B2%5D%5Bkey1%5D=value1&Values%5B2%5D%5Bkey2%5D=value2
    QueryStringBuilder.BuildQueryString(new
           {
             Age = 19,
             Name = "John&Doe",
             Values = new object[]
                      {
                        1,
                        2,
                        new Dictionary<string, string>()
                        {
                          { "key1", "value1" },
                          { "key2", "value2" },
                        }
                      },
           });
    
    // 0=1&1=2&2%5B0%5D=one&2%5B1%5D=two&2%5B2%5D=three&3%5Bkey1%5D=value1&3%5Bkey2%5D=value2
    QueryStringBuilder.BuildQueryString(new object[]
           {
             1,
             2,
             new object[] { "one", "two", "three" },
             new Dictionary<string, string>()
             {
               { "key1", "value1" },
               { "key2", "value2" },
             }
           }
      );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-10
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      相关资源
      最近更新 更多