json转换时DisplayName属性将被忽略 [英] DisplayName attribute is ignored while json conversion
本文介绍了json转换时DisplayName属性将被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下的课程
public class Person
{
public string Name { get; set; }
[DisplayName ("Please Enter Your Age")]
public int Age { get; set; }
public string Sex { get; set; }
}
我使用MVC3的json()
将这个对象序列化为Json,但是DisplayName
属性被忽略.我将json提取为
I serialized this object to Json using json()
of MVC3, but the DisplayName
attribute is ignored. I get the json as
"*{"Name":"Person Name","**Age**":28,"Sex":"Male"}*"
实际上我在期待
"*{"Name":"Person Name","**Please Enter Your Age**":28,"Sex":"Male"}*"
代码将对象转换为json
Code converts the object to json
[HttpGet]
public JsonResult JsonTest()
{
Person person = new Person();
person.Age = 28;
person.Name = "Person Name";
person.Sex = "Male";
return (Json(person, JsonRequestBehavior.AllowGet));
}
任何帮助将不胜感激!
推荐答案
您可以使用DataContractJsonSerializer
通过使用[DataMember(Name = "myOwnName")]
数据注释为属性指定不同的名称.或编写自己的序列化器.
You can use the DataContractJsonSerializer
to give different names to your properties by using the [DataMember(Name = "myOwnName")]
data annotation. Or write your own serializer.
可以找到示例此处.
这篇关于json转换时DisplayName属性将被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文