MVC3 JSON序列化:如何控制房地产的名字呢? [英] MVC3 JSON Serialization: How to control the property names?
本文介绍了MVC3 JSON序列化:如何控制房地产的名字呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想序列化一个简单对象来JSON:
I want to serialize a simple object to JSON:
public class JsonTreeNode
{
[DataMember(Name = "title")]
public string Title { get; set; }
[DataMember(Name = "isFolder")]
public bool IsFolder { get; set; }
[DataMember(Name = "key")]
public string Key { get; set; }
[DataMember(Name = "children")]
public IEnumerable<JsonTreeNode> Children { get; set; }
[DataMember(Name = "select")]
public bool SelectedOnInit { get; set; }
}
但每当我做到这一点:
But whenever I do it:
return Json(tree, JsonRequestBehavior.AllowGet);
属性名称不作为 [数据成员]
部分中指定,而是类似直接在类如中定义的在 SelectOnInit
的情况下,它是不是选择
,但 SelectOnInit
The property names are not as specified in the [DataMember]
section, but similar to the ones defined directly in the class e.g. in the case of SelectOnInit
it is not select
but SelectOnInit
.
我在做什么错了?
推荐答案
我用在这个问题中的答案提供的技术解决了这个问题:
I solved the problem by using the technique provided in the answer in this question:
<一个href=\"http://stackoverflow.com/questions/1302946/asp-net-mvc-controlling-serialization-with-jsonresult\">ASP.NET MVC:与JsonResult 属性名称的控制序列
下面是我做的类:
/// <summary>
/// Similiar to <see cref="JsonResult"/>, with
/// the exception that the <see cref="DataContract"/> attributes are
/// respected.
/// </summary>
/// <remarks>
/// Based on the excellent stackoverflow answer:
/// http://stackoverflow.com/a/263416/1039947
/// </remarks>
public class JsonDataContractActionResult : ActionResult
{
/// <summary>
/// Initializes a new instance of the class.
/// </summary>
/// <param name="data">Data to parse.</param>
public JsonDataContractActionResult(Object data)
{
Data = data;
}
/// <summary>
/// Gets or sets the data.
/// </summary>
public Object Data { get; private set; }
/// <summary>
/// Enables processing of the result of an action method by a
/// custom type that inherits from the ActionResult class.
/// </summary>
/// <param name="context">The controller context.</param>
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
var serializer = new DataContractJsonSerializer(Data.GetType());
string output;
using (var ms = new MemoryStream())
{
serializer.WriteObject(ms, Data);
output = Encoding.UTF8.GetString(ms.ToArray());
}
context.HttpContext.Response.ContentType = "application/json";
context.HttpContext.Response.Write(output);
}
}
用法:
public ActionResult TestFunction()
{
var testObject = new TestClass();
return new JsonDataContractActionResult(testObject);
}
我也不得不修改初始类:
I also had to modify the initial class:
// -- The DataContract property was added --
[DataContract]
public class JsonTreeNode
{
[DataMember(Name = "title")]
public string Title { get; set; }
[DataMember(Name = "isFolder")]
public bool IsFolder { get; set; }
[DataMember(Name = "key")]
public string Key { get; set; }
[DataMember(Name = "children")]
public IEnumerable<JsonTreeNode> Children { get; set; }
[DataMember(Name = "select")]
public bool SelectedOnInit { get; set; }
}
这篇关于MVC3 JSON序列化:如何控制房地产的名字呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文