random.choices может выбирать элементы из взвешенной совокупности. Пример:
>>> import random
>>> histogram = {"white": 1, "red": 5, "blue": 10}
>>> pixels = random.choices(list(histogram.keys()), list(histogram.values()), k=25)
>>> pixels
['blue', 'red', 'red', 'red', 'blue', 'red', 'red', 'white', 'blue', 'white', 'red', 'red', 'blue', 'red', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue']
Вот как я это сделаю в APi
public ActionResult SomeActionMethod() {
return Json(new {foo="bar", baz="Blech"});
}
Если вы хотите использовать JsonConverter и контролировать, как данные сериализуются, то
public class JsonNetResult : ActionResult
{
/// <summary>
/// Initializes a new instance of the <see cref="JsonNetResult"/> class.
/// </summary>
public JsonNetResult()
{
}
/// <summary>
/// Gets or sets the content encoding.
/// </summary>
/// <value>The content encoding.</value>
public Encoding ContentEncoding { get; set; }
/// <summary>
/// Gets or sets the type of the content.
/// </summary>
/// <value>The type of the content.</value>
public string ContentType { get; set; }
/// <summary>
/// Gets or sets the data.
/// </summary>
/// <value>The data object.</value>
public object Data { get; set; }
/// <summary>
/// Enables processing of the result of an action method by a custom type that inherits from the <see cref="T:System.Web.Mvc.ActionResult"/> class.
/// </summary>
/// <param name="context">The context in which the result is executed. The context information includes the controller, HTTP content, request context, and route data.</param>
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = !String.IsNullOrWhiteSpace(this.ContentType) ? this.ContentType : "application/json";
if (this.ContentEncoding != null)
{
response.ContentEncoding = this.ContentEncoding;
}
if (this.Data != null)
{
response.Write(JsonConvert.SerializeObject(this.Data));
}
}
}
Blockquote [117 ] blockquote>
public class CallbackJsonResult : JsonNetResult { /// <summary> /// Initializes a new instance of the <see cref="CallbackJsonResult"/> class. /// </summary> /// <param name="statusCode">The status code.</param> public CallbackJsonResult(HttpStatusCode statusCode) { this.Initialize(statusCode, null, null); } /// <summary> /// Initializes a new instance of the <see cref="CallbackJsonResult"/> class. /// </summary> /// <param name="statusCode">The status code.</param> /// <param name="description">The description.</param> public CallbackJsonResult(HttpStatusCode statusCode, string description) { this.Initialize(statusCode, description, null); } /// <summary> /// Initializes a new instance of the <see cref="CallbackJsonResult"/> class. /// </summary> /// <param name="statusCode">The status code.</param> /// <param name="data">The callback result data.</param> public CallbackJsonResult(object data, HttpStatusCode statusCode = HttpStatusCode.OK) { this.ContentType = null; this.Initialize(statusCode, null, data); } /// <summary> /// Initializes a new instance of the <see cref="CallbackJsonResult"/> class. /// </summary> /// <param name="statusCode">The status code.</param> /// <param name="description">The description.</param> /// <param name="data">The callback result data.</param> public CallbackJsonResult(HttpStatusCode statusCode, string description, object data) { this.Initialize(statusCode, description, data); } /// <summary> /// Initializes this instance. /// </summary> /// <param name="statusCode">The status code.</param> /// <param name="description">The description.</param> /// <param name="data">The callback result data.</param> private void Initialize(HttpStatusCode statusCode, string description, object data) { Data = new JsonData() { Success = statusCode == HttpStatusCode.OK, Status = (int)statusCode, Description = description, Data = data }; } } }
затем создайте расширение
blockquote>/// <summary> /// return Json Action Result /// </summary> /// <typeparam name="T"></typeparam> /// <param name="o"></param> /// <param name="JsonFormatting"></param> /// <returns></returns> public static CallbackJsonResult ViewResult<T>(this T o) { return new CallbackJsonResult(o); }
Нет APi, просто используйте созданное вами расширение
blockquote>public ActionResult SomeActionMethod() { return new { error = "", code = (int)HttpStatusCode.OK, data = leads}.ViewResult(); }
Я бы сделал этот комментарий, если бы мог, мне нужно больше респ. Тем не менее -
Попробуйте сделать List<IntegrationLead>
InegrationLead[]
, и все слэши должны избегать кавычек, и у вас будет потрясающее имя. Ура!