如何在特定的格式在ASP.NET MVC使用JSON(),无属性名称返回JSON [英] How to return JSON in specific format in ASP.NET MVC using Json() with no property names

查看:146
本文介绍了如何在特定的格式在ASP.NET MVC使用JSON(),无属性名称返回JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的JavaScript库,预计其在特定的JSON格式数据的图表 - 无属性名称。
我在我的模型中的对象,我使用返回的数据图表。这看起来如下:

I am using a charting javascript library that expects its data in a specific JSON format - without property names. I have an object in my Model that I am using to return the data to the charts. This looks as follows:

public class ChartData
{
    public string Key { get; set; }
    public int Value { get; set; }
}

这是动作看起来如下:

public ActionResult AssetsPerFloor(Guid id)
    {
        var results = from a in surveyRepository.GetAssetsForBuidling(id)
                      group a by a.Room.Floor into g
                      select new ChartData{ Key = g.Key.ToString(), Value = g.Count() };
        return Json(results);
    }

这将返回JSON格式 [{关键:主楼,值:1}]

This returns JSON in the format [{"Key":"Main Building","Value":1}]

但是,图表不需要属性名称,例如: [5,2],[6,3],[8,2]]

However, the chart requires no property names, eg: [[5, 2], [6, 3], [8, 2]]

反正我有可以返回结果这种格式。我敢肯定有一个简单的把戏,但我想不出它。

Is there anyway I can return the results in this format. I'm sure there's a simple trick to it, but I cant think of it.

推荐答案

据我的理解,它需要返回一个多维数组。试试这个:

As far as I understand, it needs to return a multi-dimensional array. Try this :

var results = 
    (from a in surveyRepository.GetAssetsForBuidling(id)
        group a by a.Room.Floor into g
        select new ChartData{ Key = g.Key.ToString(), Value = g.Count() })
        .Select(x => new string[] { x.Key, x.Value.ToString() };
return Json(results);

这篇关于如何在特定的格式在ASP.NET MVC使用JSON(),无属性名称返回JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆