Принудительное использование строчных имен свойств из Json () в ASP.NET MVC - PullRequest
80 голосов
/ 07 мая 2010

Учитывая следующий класс,

public class Result
{      
    public bool Success { get; set; }

    public string Message { get; set; }
}

Я возвращаю один из них в действии контроллера, вот так:

return Json(new Result() { Success = true, Message = "test"})

Тем не менее, моя клиентская среда ожидает, что эти свойства будут состоять из строчных букв и сообщения. Без необходимости иметь имена свойств в нижнем регистре - это ли способ достичь этой мысли обычным вызовом функции Json?

Ответы [ 5 ]

127 голосов
/ 10 мая 2010

Способ достижения этого заключается в реализации пользовательского JsonResult, как здесь: Создание пользовательского ValueType и сериализация с пользовательским JsonResult (исходная ссылка не работает) .

И используйте альтернативный сериализатор, такой как JSON.NET , который поддерживает такое поведение, например:

Product product = new Product
{
  ExpiryDate = new DateTime(2010, 12, 20, 18, 1, 0, DateTimeKind.Utc),
  Name = "Widget",
  Price = 9.99m,
  Sizes = new[] {"Small", "Medium", "Large"}
};

string json = 
  JsonConvert.SerializeObject(
    product,
    Formatting.Indented,
    new JsonSerializerSettings 
    { 
      ContractResolver = new CamelCasePropertyNamesContractResolver() 
    }
);

Результаты в

{
  "name": "Widget",
  "expiryDate": "\/Date(1292868060000)\/",
  "price": 9.99,
  "sizes": [
    "Small",
    "Medium",
    "Large"
  ]
}
11 голосов
/ 10 марта 2015

Смена сериализатора проста, если вы используете Web API, но, к сожалению, сам MVC использует JavaScriptSerializer без возможности изменить это значение для использования JSON.Net.

ответ Джеймса и ответ Даниэля дает вам гибкость JSON.Net, но означает, что везде, где вы обычно делаете return Json(obj), вы должны перейти на return new JsonNetResult(obj) или Подобное, которое, если у вас большой проект, может оказаться проблемой, а также не очень гибко, если вы передумаете на сериализаторе, который хотите использовать.


Я решил пойти по маршруту ActionFilter. Приведенный ниже код позволяет вам выполнять любые действия с использованием JsonResult и просто применять к нему атрибут для использования JSON.Net (со свойствами нижнего регистра):

[JsonNetFilter]
[HttpPost]
public ActionResult SomeJson()
{
    return Json(new { Hello = "world" });
}

// outputs: { "hello": "world" }

Вы даже можете установить это, чтобы автоматически применять ко всем действиям (с незначительным ударом по производительности при проверке is):

FilterConfig.cs

// ...
filters.Add(new JsonNetFilterAttribute());

код

public class JsonNetFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.Result is JsonResult == false)
            return;

        filterContext.Result = new CustomJsonResult((JsonResult)filterContext.Result);
    }

    private class CustomJsonResult : JsonResult
    {
        public CustomJsonResult(JsonResult jsonResult)
        {
            this.ContentEncoding = jsonResult.ContentEncoding;
            this.ContentType = jsonResult.ContentType;
            this.Data = jsonResult.Data;
            this.JsonRequestBehavior = jsonResult.JsonRequestBehavior;
            this.MaxJsonLength = jsonResult.MaxJsonLength;
            this.RecursionLimit = jsonResult.RecursionLimit;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet
                && String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
                throw new InvalidOperationException("GET not allowed! Change JsonRequestBehavior to AllowGet.");

            var response = context.HttpContext.Response;

            response.ContentType = String.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;

            if (this.ContentEncoding != null)
                response.ContentEncoding = this.ContentEncoding;

            if (this.Data != null)
            {
                var json = JsonConvert.SerializeObject(
                    this.Data,
                    new JsonSerializerSettings
                        {
                            ContractResolver = new CamelCasePropertyNamesContractResolver()
                        });

                response.Write(json);
            }
        }
    }
}
9 голосов
/ 28 марта 2014

С моим решением вы можете переименовать любое свойство, которое вы хотите.

Я нашел часть решения здесь и на SO

public class JsonNetResult : ActionResult
    {
        public Encoding ContentEncoding { get; set; }
        public string ContentType { get; set; }
        public object Data { get; set; }

        public JsonSerializerSettings SerializerSettings { get; set; }
        public Formatting Formatting { get; set; }

        public JsonNetResult(object data, Formatting formatting)
            : this(data)
        {
            Formatting = formatting;
        }

        public JsonNetResult(object data):this()
        {
            Data = data;
        }

        public JsonNetResult()
        {
            Formatting = Formatting.None;
            SerializerSettings = new JsonSerializerSettings();
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            var response = context.HttpContext.Response;
            response.ContentType = !string.IsNullOrEmpty(ContentType)
              ? ContentType
              : "application/json";
            if (ContentEncoding != null)
                response.ContentEncoding = ContentEncoding;

            if (Data == null) return;

            var writer = new JsonTextWriter(response.Output) { Formatting = Formatting };
            var serializer = JsonSerializer.Create(SerializerSettings);
            serializer.Serialize(writer, Data);
            writer.Flush();
        }
    }

Так что в моем контроллере я могу это сделать

        return new JsonNetResult(result);

В моей модели теперь я могу иметь:

    [JsonProperty(PropertyName = "n")]
    public string Name { get; set; }

Обратите внимание, что теперь вам нужно установить JsonPropertyAttribute для каждого свойства, которое вы хотите сериализовать.

1 голос
/ 20 апреля 2018

Хотя это старый вопрос, надеюсь, что приведенный ниже фрагмент кода будет полезен для других,

Я сделал это ниже с веб-API MVC5.

public JsonResult<Response> Post(Request request)
    {
        var response = new Response();

        //YOUR LOGIC IN THE METHOD
        //.......
        //.......

        return Json<Response>(response, new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver() });
    }
0 голосов
/ 30 января 2019

Вы можете добавить эту настройку к Global.asax, и она будет работать везде.

public class Global : HttpApplication
{   
    void Application_Start(object sender, EventArgs e)
    {
        //....
         JsonConvert.DefaultSettings = () =>
         {
             var settings = new JsonSerializerSettings
             {
                 ContractResolver = new CamelCasePropertyNamesContractResolver(),
                 PreserveReferencesHandling = PreserveReferencesHandling.None,
                 Formatting = Formatting.None
             };

             return settings;
         }; 
         //....
     }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...