鉴于以下课程,
public class Result
{
public bool Success { get; set; }
public string Message { get; set; }
}
我在这样的Controller动作中返回其中一个,
return Json(new Result() { Success = true, Message = "test"})
但是我的客户端框架期望这些属性是小写的成功和消息。没有实际上必须有小写的属性名称是一种方法来实现这种思想正常的Json函数调用?
答案 0 :(得分:126)
实现此目的的方法是在此处实现自定义JsonResult
:
Creating a custom ValueType and Serialising with a custom 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"
]
}
答案 1 :(得分:11)
如果您使用Web API,更改序列化程序很简单,但不幸的是,MVC本身使用JavaScriptSerializer
而无法更改此选项以使用JSON.Net。
James' answer和Daniel's answer为您提供了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);
}
}
}
}
答案 2 :(得分:9)
使用我的解决方案,您可以重命名所需的每个属性。
我已经找到了解决方案的一部分here和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
设置为要序列化的每个属性。
答案 3 :(得分:1)
虽然这是一个老问题,但希望下面的代码片段会对其他人有所帮助,
我在下面使用MVC5 Web API。
public JsonResult<Response> Post(Request request)
{
var response = new Response();
//YOUR LOGIC IN THE METHOD
//.......
//.......
return Json<Response>(response, new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver() });
}
答案 4 :(得分:0)
您可以将此设置添加到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;
};
//....
}
}