C语言中来自SQL server的JavaScript数组数据#

C语言中来自SQL server的JavaScript数组数据#,javascript,c#,sql-server,json,asp.net-mvc,Javascript,C#,Sql Server,Json,Asp.net Mvc,我试图将数据存储在我从SQL server检索到的数组中,他们是否有办法实现这一点 这里是我要动态生成的示例数组: var movies = [ { "rank": 1, "rating": 9.2, "year": 1994, "title": "The Shawshank Redemption" }, { "rank": 2, "rating": 9.2, "year": 1972, "title": "Th

我试图将数据存储在我从SQL server检索到的数组中,他们是否有办法实现这一点

这里是我要动态生成的示例数组:

var movies = [
                    { "rank": 1,  "rating": 9.2, "year": 1994, "title": "The Shawshank Redemption" },
                    { "rank": 2,  "rating": 9.2, "year": 1972, "title": "The Godfather" },
                    { "rank": 3,  "rating": 9,   "year": 1974, "title": "The Godfather: Part II" },
                    { "rank": 4,  "rating": 8.9, "year": 1966, "title": "Il buono, il brutto, il cattivo." },
                    { "rank": 5,  "rating": 8.9, "year": 1994, "title": "Pulp Fiction" },
                    { "rank": 6,  "rating": 8.9, "year": 1957, "title": "12 Angry Men" },
                    { "rank": 7,  "rating": 8.9, "year": 1993, "title": "Schindler's List" },
                    { "rank": 8,  "rating": 8.8, "year": 1975, "title": "One Flew Over the Cuckoo's Nest" },
                    { "rank": 9,  "rating": 8.8, "year": 2010, "title": "Inception" },
                    { "rank": 10, "rating": 8.8, "year": 2008, "title": "The Dark Knight" }
                ]

从SQL Server获取IEnumerable模型,并使用标准System.Web.Script.Serialization.JavaScriptSerializer对其进行序列化:

控制器:

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace WebApplication1.Controllers {
    public class HomeController : Controller {
        public ActionResult Index() {
            object model = CreateScriptObjectFromDataObject(GetModel());
            return View(model);
        }
        private string CreateScriptObjectFromDataObject(IEnumerable dataObject) {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(dataObject);
        }
        private IEnumerable GetModel() {
            List<DataItem> items = new List<DataItem>();

            //LOAD ITEMS FROM DB INSTEAD

            items.Add(new DataItem() { rank = 1, rating = 9.2, year = 1994, title = "The Shawshank Redemption" });
            items.Add(new DataItem() { rank = 2, rating = 9.0, year = 1974, title = "The Godfather" });
            items.Add(new DataItem() { rank = 3, rating = 9.2, year = 1994, title = "The Godfather: Part II" });
            items.Add(new DataItem() { rank = 4, rating = 8.9, year = 1966, title = "Il buono, il brutto, il cattivo." });
            items.Add(new DataItem() { rank = 5, rating = 8.9, year = 1994, title = "Pulp Fiction" });
            items.Add(new DataItem() { rank = 6, rating = 8.9, year = 1957, title = "The Shawshank Redemption" });
            items.Add(new DataItem() { rank = 7, rating = 8.9, year = 1993, title = "12 Angry Men" });
            items.Add(new DataItem() { rank = 8, rating = 8.8, year = 1975, title = "One Flew Over the Cuckoo's Nes" });
            items.Add(new DataItem() { rank = 9, rating = 8.8, year = 2010, title = "Inception" });
            items.Add(new DataItem() { rank = 10, rating = 8.8, year = 2008, title = "The Dark Knight" });

            return items;
        }
    }
    public class DataItem {
        public int rank { get; set; }
        public double rating { get; set; }
        public int year { get; set; }
        public string title { get; set; }
    }
}
使用系统;
使用System.Linq;
使用系统集合;
使用System.Collections.Generic;
使用System.Web.Mvc;
使用System.Web.Script.Serialization;
命名空间WebApplication1.Controllers{
公共类HomeController:控制器{
公共行动结果索引(){
对象模型=CreateScriptObjectFromDataObject(GetModel());
返回视图(模型);
}
私有字符串CreateScriptObjectFromDataObject(IEnumerable dataObject){
JavaScriptSerializer serializer=新的JavaScriptSerializer();
返回serializer.Serialize(数据对象);
}
私有IEnumerable GetModel(){
列表项=新列表();
//改为从数据库加载项
items.Add(newdataitem(){rank=1,rating=9.2,year=1994,title=“肖申克的救赎”});
Add(newdataitem(){rank=2,rating=9.0,year=1974,title=“thegoddaver”});
items.Add(newdataitem(){rank=3,rating=9.2,year=1994,title=“教父:第二部分”});
添加(新数据项(){rank=4,rating=8.9,year=1966,title=“Il buono,Il brutto,Il cattivo.”);
添加(新数据项(){rank=5,rating=8.9,year=1994,title=“纸浆小说”});
items.Add(newdataitem(){rank=6,rating=8.9,year=1957,title=“肖申克的救赎”});
添加(新数据项(){rank=7,rating=8.9,year=1993,title=“12愤怒的男人”});
items.Add(newdataitem(){rank=8,rating=8.8,year=1975,title=“一只飞过布谷鸟的巢”});
添加(newdataitem(){rank=9,rating=8.8,year=2010,title=“Inception”});
items.Add(newdataitem(){rank=10,rating=8.8,year=2008,title=“The Dark Knight”});
退货项目;
}
}
公共类数据项{
公共整数秩{get;set;}
公共双重评级{get;set;}
公共整数年{get;set;}
公共字符串标题{get;set;}
}
}
视图:


var movies=@Html.Raw(模型);

从SQL Server获取IEnumerable模型,并使用标准System.Web.Script.Serialization.JavaScriptSerializer对其进行序列化:

控制器:

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace WebApplication1.Controllers {
    public class HomeController : Controller {
        public ActionResult Index() {
            object model = CreateScriptObjectFromDataObject(GetModel());
            return View(model);
        }
        private string CreateScriptObjectFromDataObject(IEnumerable dataObject) {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(dataObject);
        }
        private IEnumerable GetModel() {
            List<DataItem> items = new List<DataItem>();

            //LOAD ITEMS FROM DB INSTEAD

            items.Add(new DataItem() { rank = 1, rating = 9.2, year = 1994, title = "The Shawshank Redemption" });
            items.Add(new DataItem() { rank = 2, rating = 9.0, year = 1974, title = "The Godfather" });
            items.Add(new DataItem() { rank = 3, rating = 9.2, year = 1994, title = "The Godfather: Part II" });
            items.Add(new DataItem() { rank = 4, rating = 8.9, year = 1966, title = "Il buono, il brutto, il cattivo." });
            items.Add(new DataItem() { rank = 5, rating = 8.9, year = 1994, title = "Pulp Fiction" });
            items.Add(new DataItem() { rank = 6, rating = 8.9, year = 1957, title = "The Shawshank Redemption" });
            items.Add(new DataItem() { rank = 7, rating = 8.9, year = 1993, title = "12 Angry Men" });
            items.Add(new DataItem() { rank = 8, rating = 8.8, year = 1975, title = "One Flew Over the Cuckoo's Nes" });
            items.Add(new DataItem() { rank = 9, rating = 8.8, year = 2010, title = "Inception" });
            items.Add(new DataItem() { rank = 10, rating = 8.8, year = 2008, title = "The Dark Knight" });

            return items;
        }
    }
    public class DataItem {
        public int rank { get; set; }
        public double rating { get; set; }
        public int year { get; set; }
        public string title { get; set; }
    }
}
使用系统;
使用System.Linq;
使用系统集合;
使用System.Collections.Generic;
使用System.Web.Mvc;
使用System.Web.Script.Serialization;
命名空间WebApplication1.Controllers{
公共类HomeController:控制器{
公共行动结果索引(){
对象模型=CreateScriptObjectFromDataObject(GetModel());
返回视图(模型);
}
私有字符串CreateScriptObjectFromDataObject(IEnumerable dataObject){
JavaScriptSerializer serializer=新的JavaScriptSerializer();
返回serializer.Serialize(数据对象);
}
私有IEnumerable GetModel(){
列表项=新列表();
//改为从数据库加载项
items.Add(newdataitem(){rank=1,rating=9.2,year=1994,title=“肖申克的救赎”});
Add(newdataitem(){rank=2,rating=9.0,year=1974,title=“thegoddaver”});
items.Add(newdataitem(){rank=3,rating=9.2,year=1994,title=“教父:第二部分”});
添加(新数据项(){rank=4,rating=8.9,year=1966,title=“Il buono,Il brutto,Il cattivo.”);
添加(新数据项(){rank=5,rating=8.9,year=1994,title=“纸浆小说”});
items.Add(newdataitem(){rank=6,rating=8.9,year=1957,title=“肖申克的救赎”});
添加(新数据项(){rank=7,rating=8.9,year=1993,title=“12愤怒的男人”});
items.Add(newdataitem(){rank=8,rating=8.8,year=1975,title=“一只飞过布谷鸟的巢”});
添加(newdataitem(){rank=9,rating=8.8,year=2010,title=“Inception”});
items.Add(newdataitem(){rank=10,rating=8.8,year=2008,title=“The Dark Knight”});
退货项目;
}
}
公共类数据项{
公共整数秩{get;set;}
公共双重评级{get;set;}
公共整数年{get;set;}
公共字符串标题{get;set;}
}
}
视图:


var movies=@Html.Raw(模型);

要在c中读取/创建json数据,请查看json.net库。要在c#中读取/创建json数据,请查看json.net库。