ASP .Net JSON和Morris.js帮助我! [英] ASP .Net JSON and Morris.js HELP ME !
问题描述
早上好,我想使用带有ASP的MORRIS.JS库。网,我知道我必须是一个JSON对象,但不是怎么做,有人帮助我吗?作为一个例子,如果我对数据库进行查询并希望以JSON格式发送数据,以便库成为MORRIS.JS图,有人可以给我一个简单的例子吗?感谢这个社区,抱歉我的英语不是很好,我会说这已经够糟了,但我会理解的!
链接到库http://www.oesmith.co.uk/morris.js/index.html [ ^ ]
Good morning, I would like to use the library MORRIS.JS with ASP. Net, I know I must be a JSON object, but not how to do it, someone help me? As an example if I do a query on a database and want to send the data in JSON format so the library becomes MORRIS.JS the graph, can someone give me a simple example? Thanks to this community and sorry my English is not very good, I would say that is bad enough, but I will understand!
Linkt to library http://www.oesmith.co.uk/morris.js/index.html[^]
推荐答案
例如,您可以将通用列表转换为json,如下所示
for example you can convert a generic list to json like below
public class Person
{
public string Name{get;set;}
public int Age{get;set;}
}
List<person> person=new List<person>();
person.Add(new Person{Name="Jemi","23"});
person.Add(new Person{Name="Jemi2","23"});
//Converting person list to jSon by using JavaScriptSerializer
JavaScriptSerializer jss = new JavaScriptSerializer(person);
string output = jss.Serialize(person);
希望这有帮助
Hope this helps
I also encountered the same problem with you,just did a project on morris example,I hope to help you.
<body>
<div>
Morris
<div id="myfirstchart" style="height: 250px; " >
</div>
</div>
</body>
</html>
<script type="text/javascript">
new Morris.Line({
element: 'myfirstchart',
data: @ViewBag.jss ,
xkey: 'year',
ykeys: ['value','a','b'],
labels: ['Value','This A','this B']
});
</script>
<pre lang="c#"> public ActionResult Morris()
{
LineDemo lineDemo1 = new LineDemo();
lineDemo1.year="2008";
lineDemo1.value = 20;
lineDemo1.a=30;
lineDemo1.b=15;
LineDemo lineDemo2 = new LineDemo();
lineDemo2.year = "2009";
lineDemo2.value = 30;
lineDemo2.a = 40;
lineDemo2.b = 20;
LineDemo lineDemo3 = new LineDemo();
lineDemo3.year = "2010";
lineDemo3.value = 40;
lineDemo3.a = 50;
lineDemo3.b = 30;
LineDemo lineDemo4 = new LineDemo();
lineDemo4.year = "2011";
lineDemo4.value = 25;
lineDemo4.a = 35;
lineDemo4.b = 55;
List<LineDemo> listLine = new List<LineDemo>();
listLine.Add(lineDemo1);
listLine.Add(lineDemo2);
listLine.Add(lineDemo3);
listLine.Add(lineDemo4);
JavaScriptSerializer jss = new JavaScriptSerializer();
string output = jss.Serialize(listLine);
string demo = output.Replace("\"", "");
ViewBag.jss = demo;
return View();
}
}
public class LineDemo
{
public string year{get;set;}
public int value{get;set;}
public int a{get;set;}
public int b{get;set;}
}
Json的数据传输,应该注意Json'Key有引号,你必须删除它,哟你可以使用正则表达式。这是asp.net MVC。我的英语不好。希望你能理解。
The data transport by Json, It should be noted that Json' Key have quotation ,you must delete it,you can use Regular Expression . this is asp.net MVC. My English is not good.Hope you can understand.
看看这个...
http://www.tapankumar.in/2014/ 03 / Creating-graph-in-aspnet-with-ajax-httphandler-and-morris.js-plugin.html [ ^ ]
Check this out...
http://www.tapankumar.in/2014/03/creating-graph-in-aspnet-with-ajax-httphandler-and-morris.js-plugin.html[^]
这篇关于ASP .Net JSON和Morris.js帮助我!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!