2

Open Flash Chart 2 を使用するアプリを作成しています。このチャートは、特定の構造を持つ JSON を受け入れる Flash オブジェクトです。

"elements": [
{
    "type": "bar_stack",
    "colours": [
        "#F19899",
        "#A6CEE3"
    ],
    "alpha": 1,
    "on-show": {
        "type": "grow-up",
        "cascade": 1,
        "delay": 0
    },
    ...

次のように単純な匿名型を使用して JSON を返しています。

return Json(new
{
    elements = new [] {
        new
        {
            type = "bar_stack",
            colours = colours.Take(variables.Count()),
            alpha = 1,
            on_show = new
            {
                type = "grow-up",
                cascade = 1,
                delay = 0
            },
            ...
        }
}

問題は、いくつかのプロパティ (「on-show」など) がダッシュを使用しており、明らかに C# コードでプロパティに名前を付けるときにダッシュを使用できないことです。

これを克服する方法はありますか?できれば、一連のクラス全体を宣言する必要はありません。

4

2 に答える 2

1

辞書を使用できます:

return Json(new {
    elements = new [] {
        new Dictionary<string, object> 
        { 
            { "type", "bar_stack" },
            { "colours", new [] { "#F19899", "#A6CEE3" } },
            { "alpha", 1 },
            { "on-show", new 
                         {
                             type = "grow-up",
                             cascade = 1,
                             delay = 0
                         } },
        } 
    }
});

(SO エディターで書かれています。構文エラーがいくつかあるかもしれませんが、お分かりいただけると思います....)

于 2012-08-24T14:54:13.050 に答える
0

クレイグのソリューションはおそらくより良いですが、その間に私はこれを実装しました:

public class UnderscoreToDashAttribute : ActionFilterAttribute
{
    private readonly string[] _fixes;

    public UnderscoreToDashAttribute(params string[] fixes)
    {
        _fixes = fixes;
    }

    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Filter = new ReplaceFilter(filterContext, s => _fixes.Aggregate(s, (current, fix) => current.Replace(fix, fix.Replace('_', '-'))));
    }

    public class ReplaceFilter : MemoryStream
    {
        private readonly Stream _stream;
        private readonly Func<string, string> _filter;

        public ReplaceFilter(ControllerContext filterContext, Func<string, string> filter)
        {
            _stream = filterContext.HttpContext.Response.Filter;
            _filter = filter;
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
             // capture the data and convert to string 
            var data = new byte[count];
            Buffer.BlockCopy(buffer, offset, data, 0, count);

            var s = _filter(Encoding.Default.GetString(buffer));

            // write the data to stream 
            var outdata = Encoding.Default.GetBytes(s);
            _stream.Write(outdata, 0, outdata.GetLength(0));
        }
    }
}

次に、アクションを次のように装飾する場合:

[UnderscoreToDash("on_show", "grid_colour")]
public JsonResult GetData()

適切な「修正」を行います。

PSResharperがコードをLinqに変更する素晴らしい瞬間...

_fixes.Aggregate(s, (current, fix) => current.Replace(fix, fix.Replace('_', '-')))
于 2012-08-24T15:05:42.517 に答える