将二维C#列表字符串转换为二维Javascript Json数组

时间:2019-07-12 15:18:00

标签: javascript c# asp.net-mvc

我的C#代码中有一个二维列表字符串,这是我从数据库中读取的数据。看起来如下。

{ {"Item 1A","Item 1B","Item 1C"}, {"Item 2A","Item 2B","Item 2C"}, {"Item 3A","Item 3B","Item 3C"} }

我正在从index.cshtml网页中的脚本调用此函数,并且需要使用以下格式作为Json。

[ ["Item 1A","Item 1B","Item 1C"], ["Item 2A","Item 2B","Item 2C"], ["Item 3A","Item 3B","Item 3C"] ]

下面的C#代码示例

public static List<List<string>> GetData()
    {
        List<List<string>> projectsList = new List<List<string>>();

        using (var database = new DatabaseEntities())
        {
            var projects = database.Tables;

            foreach (Table project in database.Tables)
            { 
                string projectName = project.Project_Name;
                string latitude = project.Latitude;
                string longitude = project.Latitude;

                projectsList.Add(new List<string>() { projectName, latitude, longitude } );
            }
        }

        return projectsList;
    }
}

Index.cshtml中的Javascript

var projects = @MvcApplication.GetData();

更新:

通过更改以下内容解决了该问题:

return JsonConvert.SerializeObject(projectsList,Formatting.Indented);

@Html.Raw(MvcApplication.GetData());

0 个答案:

没有答案