赞
踩
step1:新建立一个mvc工程,建立相应的controller(此处按默认的Home建立了HomeController)
step2:修改HomeController中代码,如下:
- public ActionResult Index()
- {
- var data = new List<Object>();
- return Json(data, JsonRequestBehavior.AllowGet);
- }
此时返回的结果 :
对应的json视图:
step3:将代码更改为
- public ActionResult Index()
- {
-
- var buildName = new List<Object>();
- var lineChart = new List<Object>();
- var data = new List<Object>();
-
- data.Add(new
- {
- Buildname = buildName,
- Linechart = lineChart
- });
- return Json(data, JsonRequestBehavior.AllowGet);
- }
效果如下:
step4:向buildName数组添加内容(数组形式)
代码:
- public ActionResult Index()
- {
- int[] table = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- var buildName = new List<Object>();
- var lineChart = new List<Object>();
- var data = new List<Object>();
-
- //向buildName中添加数组0,数组0内的元素为number类型
- var p = from p1 in table
- where p1 % 2 == 0
- select p1;
- buildName.Add(p);
-
- //向buildName中添加数组1,数组1内的元素为Object类型
- var q = from q1 in table
- where q1 % 3 == 0
- select new { id = q1 };
- buildName.Add(q);
-
- //向buildName中添加对象,对象的内同为数组1
- buildName.Add(new {id=q});
-
- data.Add(new
- {
- Buildname = buildName,
- Linechart = lineChart
- });
- return Json(data, JsonRequestBehavior.AllowGet);
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
效果如下:
step5:向lineChart数组添加内容(数组形式)
①建立一个student类
- class Student
- {
- public int id { get; set; }
- public String name { get; set; }
- }
②建立student容器,实例化并赋值
- //建立student容器,并添加对象
- List<Student> s = new List<Student>();
- s.Add(new Student() { id = 1, name = "张三" });
- s.Add(new Student() { id = 2, name = "李四" });
- s.Add(new Student() { id = 3, name = "王五" });
③建立一个Employee类
- class Employee
- {
- public int e_id { get; set; }
- public String e_name { get; set; }
- }
④建立Employee容器,实例化并赋值
- List<Employee> s1 = new List<Employee>();
- s1.Add(new Employee() { e_id = 1, e_name = "工_张三" });
- s1.Add(new Employee() { e_id = 2, e_name = "工_李四" });
- s1.Add(new Employee() { e_id = 3, e_name = "工_王五" });
⑤筛选信息,并将info添加至lineChart数组中
- var info =
- from w in s
- join w1 in s1 on w.id equals w1.e_id
- where w.id == 1 || w.id == 2
- select new
- {
- id = w.id,
- stu_name=w.name,
- emp_name=w1.e_name
- };
- lineChart.Add(info);
完整代码如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace jsonTest1.Controllers
- {
- class Student
- {
- public int id { get; set; }
- public String name { get; set; }
- }
-
- class Employee
- {
- public int e_id { get; set; }
- public String e_name { get; set; }
- }
-
- public class HomeController : Controller
- {
- //
- // GET: /Home/
-
- public ActionResult Index()
- {
- int[] table = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- var buildName = new List<Object>();
- var lineChart = new List<Object>();
- var data = new List<Object>();
-
- //向buildName中添加数组0,数组0内的元素为number类型
- var p = from p1 in table
- where p1 % 2 == 0
- select p1;
- buildName.Add(p);
-
- //向buildName中添加数组1,数组1内的元素为Object类型
- var q = from q1 in table
- where q1 % 3 == 0
- select new { id = q1 };
- buildName.Add(q);
-
- //向buildName中添加对象,对象的内同为数组1
- buildName.Add(new {id=q});
-
- //建立student容器,并添加对象
- List<Student> s = new List<Student>();
- s.Add(new Student() { id = 1, name = "张三" });
- s.Add(new Student() { id = 2, name = "李四" });
- s.Add(new Student() { id = 3, name = "王五" });
-
- //建立Employee容器,并添加对象
- List<Employee> s1 = new List<Employee>();
- s1.Add(new Employee() { e_id = 1, e_name = "工_张三" });
- s1.Add(new Employee() { e_id = 2, e_name = "工_李四" });
- s1.Add(new Employee() { e_id = 3, e_name = "工_王五" });
-
- //筛选信息,并将info添加至lineChart数组中
- var info =
- from w in s
- join w1 in s1 on w.id equals w1.e_id
- where w.id == 1 || w.id == 2
- select new
- {
- id = w.id,
- stu_name=w.name,
- emp_name=w1.e_name
- };
- lineChart.Add(info);
-
- data.Add(new
- {
- Buildname = buildName,
- Linechart = lineChart
- });
- return Json(data, JsonRequestBehavior.AllowGet);
- }
-
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
效果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。