JSON 序列化,返回带有破折号的键?

发布于 2024-10-24 00:50:04 字数 684 浏览 3 评论 0原文

我想从我的控制器返回 JSON,该控制器是从匿名类型生成的,并且键名称中包含破折号。这可能吗?

因此,如果我有这个:

public ActionResult GetJSONData() {
    var data = new { DataModifiedDate = myDate.ToShortDateString() };
    return Json(data);
}

在客户端,我希望它像这样序列化:

{ "data-modified-date" : "3/17/2011" }

我想要这个的原因是这个 Json 数据最终将成为 DOM 节点上的属性,并且我想玩得很好并使用新的 HTML5 数据属性。我可以返回 {modifieddate: "3/17/2011" } 并以这种方式使用它,但如果我可以变得更符合我想要的标准。

据我所知,如果我编写自己的 JsonResult 类,该类在非匿名类型上使用 WCF JSON Serializer,我可以使用 DataMemberAttribute 来完成此操作。但对于这样一个简单的愿望来说,这是很大的开销。

我也可以让客户端在收到数据后为我按摩密钥,但我也希望避免这种情况。总而言之,我宁愿不遵循标准,也不愿遵循这些解决方法。

I would like to return JSON from my controller which was generated from an anonymous type and contains dashes in the key names. Is this possible?

So if I have this:

public ActionResult GetJSONData() {
    var data = new { DataModifiedDate = myDate.ToShortDateString() };
    return Json(data);
}

On the client side I would like it to arrive serialized like this:

{ "data-modified-date" : "3/17/2011" }

My reason for wanting this is this Json data will ultimately become attributes on a DOM node, and I want to play nice and use the new HTML5 data attributes. I can just return { modifieddate: "3/17/2011" } and use it this way, but if I can become that little bit more conforming to standards I'd like to be.

I understand if I write my own JsonResult class that uses the WCF JSON Serializer on a non anonymous type, I can use theDataMemberAttribute to accomplish this. But that's a lot of overhead for such a simple desire.

I could also have the client massage the keys for me once it receives the data, but I'm hoping to avoid that too. All in all I'd rather just not follow standards than either of these workarounds.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聽兲甴掵 2024-10-31 00:50:04

您可以使用 Json.NET 并完全控制属性名称:

public ActionResult GetJSONData() 
{
    var obj = new JObject();
    obj["data-modified-date"] = myDate.ToShortDateString();
    var result = JsonConvert.SerializeObject(obj);
    return Content(result, "application/json");
}

显然,这段代码迫切需要通过引入来改进自定义操作结果:

public class JsonNetResult : ActionResult
{
    private readonly JObject _jObject;
    public JsonNetResult(JObject jObject)
    {
        _jObject = jObject;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;
        response.ContentType = "application/json";
        response.Write(JsonConvert.SerializeObject(_jObject));
    }
}

然后:

public ActionResult GetJSONData() 
{
    var obj = new JObject();
    obj["data-modified-date"] = myDate.ToShortDateString();
    return new JsonNetResult(obj);
}

You could use Json.NET and have full control over property names:

public ActionResult GetJSONData() 
{
    var obj = new JObject();
    obj["data-modified-date"] = myDate.ToShortDateString();
    var result = JsonConvert.SerializeObject(obj);
    return Content(result, "application/json");
}

Obviously this code is screaming to be improved by introducing a custom action result:

public class JsonNetResult : ActionResult
{
    private readonly JObject _jObject;
    public JsonNetResult(JObject jObject)
    {
        _jObject = jObject;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;
        response.ContentType = "application/json";
        response.Write(JsonConvert.SerializeObject(_jObject));
    }
}

and then:

public ActionResult GetJSONData() 
{
    var obj = new JObject();
    obj["data-modified-date"] = myDate.ToShortDateString();
    return new JsonNetResult(obj);
}
清泪尽 2024-10-31 00:50:04

我发现 JsonResult 使用的 JavaScriptSerializer 对于字典有一个特殊情况。因此,如果您这样做:

var data = new Dictionary<string, string> 
{
     { "data-modified-date", myDate.ToShortDateString() }
};

那么生成的 JSON 就是所需的格式。

I found the JavaScriptSerializer that JsonResult uses has a special case for Dictionaries. So if you just do:

var data = new Dictionary<string, string> 
{
     { "data-modified-date", myDate.ToShortDateString() }
};

Then the resulting JSON is in the desired format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文