如何将具有对象列表作为参数的对象序列化为查询字符串格式?
How do I serialize an object that has a list of objects as a parameter into query-string format?
我有一个对象,我需要将其属性序列化为查询字符串,以便我可以将请求发送到服务器,我找到了一个部分解决我的问题的解决方案,它序列化了除对象列表之外的所有内容我的 class 的参数,因此是问题所在。
这是序列化除对象列表之外的所有内容的代码。
public static string ToQueryString(this object request, string separator = ",")
{
if (request == null)
throw new ArgumentNullException("request");
// Get all properties on the object
var properties = request.GetType().GetProperties()
.Where(x => x.CanRead)
.Where(x => x.GetValue(request, null) != null)
.ToDictionary(x => x.Name, x => x.GetValue(request, null));
// Get names for all IEnumerable properties (excl. string)
var propertyNames = properties
.Where(x => !(x.Value is string) && x.Value is IEnumerable)
.Select(x => x.Key)
.ToList();
// Concat all IEnumerable properties into a comma separated string
foreach (var key in propertyNames)
{
var valueType = properties[key].GetType();
var valueElemType = valueType.IsGenericType
? valueType.GetGenericArguments()[0]
: valueType.GetElementType();
if (valueElemType.IsPrimitive || valueElemType == typeof(string))
{
var enumerable = properties[key] as IEnumerable;
properties[key] = string.Join(separator, enumerable.Cast<object>());
}
}
// Concat all key/value pairs into a string separated by ampersand
return string.Join("&", properties
.Select(x => string.Concat(
Uri.EscapeDataString(x.Key), "=",
Uri.EscapeDataString(x.Value.ToString()))));
}
这是我需要序列化的 class 示例:
public class Criteria
{
public int? TestInt { get; set; }
public List<int> ListaInt { get; set; }
public List<DokumentAttributeSearchCriteria> DokumentCriterias { get; set; }
public string TestString { get; set; }
public DokumentAttributeSearchCriteria DokumentCriteria { get; set; }
}
有没有人知道如何扩展这个函数以便它也适用于对象参数列表?
我会使用 Newtonsoft.Json 和 HTTP POST(使用 JSON content-body)来发送这样的嵌套对象,因为没有标准的方法来表示列表使用 query-string 个参数的对象
如果 GET 是您唯一的选择,您可以尝试这样的操作:
Uri.EscapeDataString(JsonConvert.SerializeObject(criteria))
将它转换回另一端的对象将与此相反
JsonConvert.DeserializeObject(Uri.UnescapeDataString(queryString))
我有一个对象,我需要将其属性序列化为查询字符串,以便我可以将请求发送到服务器,我找到了一个部分解决我的问题的解决方案,它序列化了除对象列表之外的所有内容我的 class 的参数,因此是问题所在。 这是序列化除对象列表之外的所有内容的代码。
public static string ToQueryString(this object request, string separator = ",")
{
if (request == null)
throw new ArgumentNullException("request");
// Get all properties on the object
var properties = request.GetType().GetProperties()
.Where(x => x.CanRead)
.Where(x => x.GetValue(request, null) != null)
.ToDictionary(x => x.Name, x => x.GetValue(request, null));
// Get names for all IEnumerable properties (excl. string)
var propertyNames = properties
.Where(x => !(x.Value is string) && x.Value is IEnumerable)
.Select(x => x.Key)
.ToList();
// Concat all IEnumerable properties into a comma separated string
foreach (var key in propertyNames)
{
var valueType = properties[key].GetType();
var valueElemType = valueType.IsGenericType
? valueType.GetGenericArguments()[0]
: valueType.GetElementType();
if (valueElemType.IsPrimitive || valueElemType == typeof(string))
{
var enumerable = properties[key] as IEnumerable;
properties[key] = string.Join(separator, enumerable.Cast<object>());
}
}
// Concat all key/value pairs into a string separated by ampersand
return string.Join("&", properties
.Select(x => string.Concat(
Uri.EscapeDataString(x.Key), "=",
Uri.EscapeDataString(x.Value.ToString()))));
}
这是我需要序列化的 class 示例:
public class Criteria
{
public int? TestInt { get; set; }
public List<int> ListaInt { get; set; }
public List<DokumentAttributeSearchCriteria> DokumentCriterias { get; set; }
public string TestString { get; set; }
public DokumentAttributeSearchCriteria DokumentCriteria { get; set; }
}
有没有人知道如何扩展这个函数以便它也适用于对象参数列表?
我会使用 Newtonsoft.Json 和 HTTP POST(使用 JSON content-body)来发送这样的嵌套对象,因为没有标准的方法来表示列表使用 query-string 个参数的对象
如果 GET 是您唯一的选择,您可以尝试这样的操作:
Uri.EscapeDataString(JsonConvert.SerializeObject(criteria))
将它转换回另一端的对象将与此相反
JsonConvert.DeserializeObject(Uri.UnescapeDataString(queryString))