Json 의 출력을 제어할 수 있는 방법이 '' 어떻게 '와' 스마를레멘테트리부트 제슨레술트 특성과 비슷하게 사용할 수 있는 XML 직렬화하지 베르트랑 출력을 제어할 수 있다.
예를 들어, 다음 주어진 클래스:
public class Foo
{
[SomeJsonSerializationAttribute("bar")]
public String Bar { get; set; }
[SomeJsonSerializationAttribute("oygevalt")]
public String Oygevalt { get; set; }
}
{ bar: '', oygevalt: '' }
반대로:
{ Bar: '', Oygevalt: '' }
뭔가 내가 원하는 것보다 조금 더 굽는다 꽂으십시오 제안됩니다 도왔으매 here& s what I did: # 39, 프레임워크입니다 재럿
public class JsonDataContractActionResult : ActionResult
{
public JsonDataContractActionResult(Object data)
{
this.Data = data;
}
public Object Data { get; private set; }
public override void ExecuteResult(ControllerContext context)
{
var serializer = new DataContractJsonSerializer(this.Data.GetType());
String output = String.Empty;
using (var ms = new MemoryStream())
{
serializer.WriteObject(ms, this.Data);
output = Encoding.Default.GetString(ms.ToArray());
}
context.HttpContext.Response.ContentType = "application/json";
context.HttpContext.Response.Write(output);
}
}
public ActionResult JsonContract(Object data)
{
return new JsonDataContractActionResult(data);
}
public ActionResult Update(String id, [Bind(Exclude="Id")] Advertiser advertiser)
{
Int32 advertiserId;
if (Int32.TryParse(id, out advertiserId))
{
// update
}
else
{
// insert
}
return JsonContract(advertiser);
}
using System;
using System.Runtime.Serialization.Json;
using System.Web.Mvc;
public class JsonDataContractActionResult : JsonResult
{
public JsonDataContractActionResult( Object data )
{
this.Data = data;
}
public override void ExecuteResult( ControllerContext context )
{
var serializer = new DataContractJsonSerializer( this.Data.GetType() );
context.HttpContext.Response.ContentType = "application/json";
serializer.WriteObject( context.HttpContext.Response.OutputStream,
this.Data );
}
}
이것은 오래된 질문을 피하는 방법을 찾고 있지만 그냥 내가 아는 그 특성 (properties) 을 사용하여 기술입니까 스크리티그너스트리부트 에서 시스템.웹스스크리pt.즈리얼리제이션스 이름공간이 시리얼화된 안타깝게도 아직 can& 설정 제어, t # 39 이 헬프룰 직렬화된 특성는 하지만 누군가 이름을 알 수 있습니다.
public class MyClass {
[ScriptIgnoreAttribute]
public bool PropertyNotSerialized { get; set; }
public bool AnyProperty { get; set; }
}
Json 결과를 출력할 는 다음과 같다.
{"AnyProperty ": false}
이 솔루션은 뉴턴소프트 사용하려면 이종오네 (성능)
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; }
참고로 지금 설정할 수 있습니다 '속성' 모든 제슨프로페르티아트리부트 직렬화할 싶어요.
쉬운 대답: '이' 와 '이' 다타콘트레이스티송즈리얼리처 존중해야 한다는 ' [다타콘트레이스트] [다모럼버]' 의 속성을 시스템.런타임.즈리얼리제이션 이름공간이 바클.