JSON新手入门

在三月份的时候做过一个很小的功能,就是从数据库中获取数据,然后通过json把数据传递到iphone客户端上面,不知道iphone开发的项目是否都需要使用json,我做的只是提供一个数据交互的接口而已,在VS2010下面建了一个C#的MVC工程,把之前项目管理系统的框架拿来改了改,就交工了。


json超哥是知道的,我之前也不知道,但用过后发现其实挺好用,无非是数据传输的一种格式罢了,通常使用的就是使用list进行数据封装,这里列出工程中所写的一些代码:


视频点播函数,在controller下面建了一个类进行存放,里面一些数据库访问的代码可以忽略

////点播
        [HttpGet]
        public JsonResult Vod(int? page)
        {
            var page1 = page ?? 0;

            //节目表
            AssetModel asset = new AssetModel();

            AssetDAL assetop = new AssetDAL();
            FilterAsset filter = new FilterAsset();

            filter.type = 0;

            DataSet ds = assetop.Query(filter, asset);

            int total = ds.Tables[0].Rows.Count;

            filter.page = page1;

            ds = assetop.Query(filter, asset);


            //类型表
            CategoryModel category = new CategoryModel();

            CategoryDAL categoryop = new CategoryDAL();

            //节目-类型表
            SortModel sort = new SortModel();

            SortDAL sortop = new SortDAL();

            FilterSort filters = new FilterSort();

            var list = new List();

            var list2 = new List();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                int id = Convert.ToInt32(ds.Tables[0].Rows[i]["编号"].ToString());
                string title = ds.Tables[0].Rows[i]["标题"].ToString();
                string poster = ds.Tables[0].Rows[i]["海报地址"].ToString();
                string image = ds.Tables[0].Rows[i]["图片地址"].ToString();
                int rating = Convert.ToInt32(ds.Tables[0].Rows[i]["评分"].ToString());

                filters.assetid = id;

                DataSet ds2 = sortop.Query(filters, sort);

                string upset = "";

                for (int k = 0; k < ds2.Tables[0].Rows.Count; k++)
                {
                    upset += categoryop.GetName(Convert.ToInt32(ds2.Tables[0].Rows[k]["类型号"].ToString())) + ",";
                }

                string categorys = upset;

                var asset1 = new TitleAsset() { AssetId = id, Title = title, Poster = poster, Image = image, Rating = rating, Categories = upset };

                list2.Add(asset1); 

            }

            var vod1 = new Vod() { Total = total, Assets = list2 };

            list.Add(vod1);

            return Json(list, JsonRequestBehavior.AllowGet);            

        }


然后是一些数据实体类,考虑到嵌套,层次关系,一个类中也会包含各种list

 //首页上面的数据
    public class TitleAsset
    {
        //数据成员
        public int AssetId { get; set; }
        public string Title { get; set; }
        public string Poster { get; set; }
        public string Image { get; set; }
        public int Rating { get; set; }
        public string Categories { get; set; }
        public string Description { get; set; }
    }

    //首页上面的分组
    public class Group
    {
        public string Name { get; set; }
        public List Assets = new List();
    }

    //直播节目表
    public class Live
    {
        public int Total { get; set; }
        public List Assets = new List();
    }

    //点播节目表
    public class Vod
    {
        public int Total { get; set; }
        public List Assets = new List();
    }

    //节目详细
    public class Detail
    {
        public List Assets = new List();
        public List Medadatas = new List();
        public string constent { get; set; }
 
    }

其实数据封装的内容就是以下几行代码而已,数据嵌套就是先定义list进行赋值,再把当前list包含在另一个list之中罢了

  var list2 = new List();

            var meta1 = new Metadata() { Name = director, Value = actors };

            list2.Add(meta1);


            //m3u8文件表
            ConstentModel constent = new ConstentModel();

            ConstentDAL conop = new ConstentDAL();


            string constents = conop.GetUrl(id);

            var list = new List();

            var detail1 = new Detail() { Assets = list1, Medadatas = list2, constent = constents };

            list.Add(detail1);


            return Json(list, JsonRequestBehavior.AllowGet);  

测试的时候在浏览器中输入  http://localhost:1980/asset

页面上会给出查询的结果,用json表示的,chrome下看上去不是很友好

[{"Assets":[{"AssetId":1,"Title":"碟中谍4","Poster":"123","Image":"123","Rating":1,"Categories":"剧情,动作,冒险,","Description":null},{"AssetId":2,"Title":"碟中谍3","Poster":"123","Image":"123","Rating":1,"Categories":"","Description":null}],"Name":"外国片"},{"Assets":[],"Name":"华语片"}]





PS:VS2010确实好用,尤其对MVC这种架构,居然其中自动嵌入了javascript,看来微软也不全是狂妄自大只支持自己的产品的。

你可能感兴趣的:(Felven在职场)