C# JSON序列化后动态属性中缺少值

C# JSON序列化后动态属性中缺少值,c#,asp.net-mvc,asp.net-web-api,json.net,C#,Asp.net Mvc,Asp.net Web Api,Json.net,我无法使用Web API控制器全局配置我的ASP.NET MVC 4应用程序,以便使用Json字符串返回HttpResponseMessage的操作在动态属性上包含Newtonsoft.Json.Linq.JValue类型的值 我可以通过在每个操作中手动序列化JSON字符串来生成所需的输出,但理想情况下,我希望能够在应用程序启动时全局配置正确的行为 演示我的问题,考虑下面的类: public class Content : IAccountEntity { public string S

我无法使用Web API控制器全局配置我的ASP.NET MVC 4应用程序,以便使用Json字符串返回HttpResponseMessage的操作在动态属性上包含Newtonsoft.Json.Linq.JValue类型的值

我可以通过在每个操作中手动序列化JSON字符串来生成所需的输出,但理想情况下,我希望能够在应用程序启动时全局配置正确的行为

演示我的问题,考虑下面的类:

public class Content : IAccountEntity
{
    public string StringProperty { get; set; }

    public dynamic DynamicProperty { get; set; }
}
给定以下全局序列化配置:

protected void Application_Start()
{
    ...

    ConfigureJsonFormat();
}

private static void ConfigureJsonFormat()
{    
    var formatters = GlobalConfiguration.Configuration.Formatters;

    formatters.Remove(formatters.XmlFormatter);

    var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;

    json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
    json.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
    json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
以及下面的MVC操作代码

public HttpResponseMessage Get(HttpRequestMessage request)
{
    ... // Other initialisation code

    content.StringProperty = "Hello world";
    content.DynamicProperty.Email = new Newtonsoft.Json.Linq.JValue("test@test.com");

    return Request.CreateResponse(HttpStatusCode.OK, content);
}
我得到以下Json响应。如您所见,它缺少动态子属性“email”的值

我可以通过采用更手动的方法创建JSON字符串响应来解决每个操作的问题,如下所示:

public HttpResponseMessage Get(HttpRequestMessage request)
{
    ... // Other initialisation code

    content.StringProperty = "Hello world";
    content.DynamicProperty.Email = new Newtonsoft.Json.Linq.JValue("test@test.com");

    var jsonSerializerSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };

    string jsonStr = JsonConvert.SerializeObject(content, Formatting.Indented, jsonSerializerSettings);

    return new HttpResponseMessage(HttpStatusCode.OK)
               {
                    Content = new StringContent(jsonStr, Encoding.UTF8, "application/json")
               };
}
它产生所需的输出:

[
  {
    "stringProperty": "Hello world",
    "dynamicProperty": {
        email: "test@test.com"
    }
  }
]
然而,我更希望能够使用全局配置复制上述行为

是否有一种方法可以修改我的方法ConfigureJsonFormat,以便在Json响应中包含动力学的值,而不必逐个操作进行编码


我希望可以在json.SerializerSettings上设置一些内容,但目前还无法找到正确的设置。

我无法重现此问题。你能展示一下DynamicProperty的初始化代码吗?另外,电子邮件属性被序列化为空数组也是非常奇怪的。我怀疑这里还发生了其他事情。进一步研究,我发现我最初对DynamicProperty.Email的类型做出了错误的假设。我原以为它是一个字符串,但实际上它是Newtonsoft.Json.Linq.JValue,它是IEnumerable。真正的代码在从数据存储MongoDb进行反序列化时创建此值,但我已经使用content.DynamicContent.email=new Newtonsoft.Json.Linq进行了手动赋值测试。JValuetest@test.com . 问题在于序列化行为不同,这取决于我是手动序列化还是依赖于全局配置。为什么必须将电子邮件属性设置为JValue?无法执行content.DynamicProperty.Email=test@test.com ?
[
  {
    "stringProperty": "Hello world",
    "dynamicProperty": {
        email: "test@test.com"
    }
  }
]