【问题标题】:JSON.NET MVC 4 WebApiJSON.NET MVC 4 WebApi
【发布时间】:2013-07-03 15:31:19
【问题描述】:

我正在创建控制器以返回 JSON 格式的对象。 我想使用 Newtonsoft.Json 库。 我创建了两种方法:

[HttpGet]
[ActionName("RetrieveTestClassJson")]
public ActionResult RetrieveTestClassJson(int id)
{
    TestThing testThing = new TestThing() { Name = "LPL.22.334", Type = TypeTest.Component };
    JsonNetResult jsonNetResult = new JsonNetResult();
    jsonNetResult.Formatting = Formatting.Indented;
    jsonNetResult.ContentType = "application/json";
    jsonNetResult.ContentEncoding = Encoding.Unicode;
    jsonNetResult.Data = testThing;
    return jsonNetResult;
}

[HttpGet]
[ActionName("RetrieveTestClassCase2")]
public TestThing RetrieveTestClassCase2(int id)
{
    TestThing testThing = new TestThing() { Name = "LPL.22.334", Type = TypeTest.Component };
    return testThing;
}

当我从 ajax 或浏览器 url 调用 RetrieveTestClassJson 时,我得到:

{"ContentEncoding":{"isThrowException":false,"bigEndian":false,"byteOrderMark":true,"m_codePage":1200,"dataItem":null,"encoderFallback":{"strDefault":"�","bIsMicrosoftBestFitFallback":false},"decoderFallback":{"strDefault":"�","bIsMicrosoftBestFitFallback":false},"m_isReadOnly":true},"ContentType":"application/json","Data":{"Name":"LPL.22.334"},"SerializerSettings":{"ReferenceLoopHandling":0,"MissingMemberHandling":0,"ObjectCreationHandling":0,"NullValueHandling":0,"DefaultValueHandling":0,"Converters":[{"CamelCaseText":true,"CanRead":true,"CanWrite":true}],"PreserveReferencesHandling":0,"TypeNameHandling":0,"TypeNameAssemblyFormat":0,"ConstructorHandling":0,"ContractResolver":null,"ReferenceResolver":null,"TraceWriter":null,"Binder":null,"Error":null,"Context":{"m_additionalContext":null,"m_state":0},"DateFormatString":"yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK","MaxDepth":null,"Formatting":0,"DateFormatHandling":0,"DateTimeZoneHandling":3,"DateParseHandling":1,"FloatFormatHandling":0,"FloatParseHandling":0,"StringEscapeHandling":0,"Culture":"(Default)","CheckAdditionalContent":false},"Formatting":1}

当我调用 RetrieveTestClassCase2 时,我得到了正常的 JSON 格式。 我现在出于测试目的删除了 xml 处理程序:

var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

我对发生的事情感到困惑。

【问题讨论】:

    标签: c# json asp.net-mvc-4 json.net


    【解决方案1】:

    WebApi 已经有 Json 序列化器在管道中,http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization,所以我认为在你显式序列化的方法中,结果最终被序列化了两次。第二种方法是您想要的,除非您也删除了 json 格式化程序。

    【讨论】:

    【解决方案2】:

    当你什么都不做时它返回 JSON 的原因是 MVC 在 JSON 序列化器中有自己的构建。因为这是一个

    [System.Web.Http.HttpGet]
    

    不是

    [System.Web.Mvc.HttpGet]
    

    它将您发送的内容视为数据而不是标记。这就是为什么它将您的对象作为 JSON 返回的原因。它知道您发送的只是一个对象,因此它会为您序列化它。如果您想在 [System.Web.Mvc.HttpGet] 中获得相同的功能,您将返回 JsonResult 而不是 ActionResult。

    var result = new JsonResult();
    result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
    result.Data = testThing;
    

    【讨论】:

      猜你喜欢
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      • 2012-04-07
      相关资源
      最近更新 更多