我怎么能得到这个输出 [英] How i can i get this output
本文介绍了我怎么能得到这个输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要输出
结果= [对象,对象,对象,对象,对象,对象]
输出来源
result =[{label:11-03-2016,value:159,year : 2000},{ 标签: 2016年12月3日, 值: 3207, 年: 2001},{ 标签: 13-03-2016,值 : 4610\" , 年: 2002},{ 标签: 14-03-2016, 值: 3418, 年: 2003},{ 标签 :15-03-2016,value:551,year:2004}]
我尝试过的:
public JsonResult GraphData()
{
GraphModel oGraphModel = null ;
列表< graphmodel> lstGraphModel = new List< graphmodel>();
int index = 0 ;
Random random = new Random();
while (index < 5 )
{
oGraphModel = new GraphModel();
oGraphModel.label = DateTime.Now.AddDays(index).ToString( dd-MM-yyyy 跨度>);
oGraphModel。 value = random.Next(index, 5000 )。ToString();
oGraphModel.year =(index + 2000 )。ToString();
lstGraphModel.Add(oGraphModel);
index ++;
}
var jsonSerializer = new JavaScriptSerializer();
string data = jsonSerializer.Serialize(lstGraphModel);
return Json(data,JsonRequestBehavior.AllowGet);
}
< script type = text / javascript >
$(document).ready(function(){
new Morris.Line ({
元素:' line-chart',
data:Graph( ),
xkey:' year',
ykeys:[' value'],
async : true ,
标签:[' value' ],
lineColors:[' #D9534F'],
lineWidth:' 2px',
hideHover: true
});
new Morris.Area({
element:' < span class =code-string> area-chart',
data:Graph(),
xkey:' < span class =code-string> year',
ykeys:[' value' ],
async : true ,
标签:[< span class =code-string>' value'],
lineColors:[' #428BCA'],
lineWidth:' 2px',
hideHover: true
});
});
函数Graph(){
var data = < span class =code-string>;
$ .ajax({
type:' GET',
url:' @ Url.Action(GraphData,Home)',
dataType:' json',
async : true ,
contentType: application / json; charset = utf-8,
data:{},
success:function(result){
data = result;
} ,
错误:函数(xhr,状态,错误){
alert(错误);
}
});
返回数据;
}
< / script >
解决方案
(document).ready(function(){
new Morris.Line({
element:' line-chart' ,
数据:Graph(),
xkey:' year',
ykeys:[' value'],
async : true ,
标签:[' value'],
lineColors:[' #D9534F'],
lin eWidth:' 2px',
hideHover: true
});
new Morris.Area({
element:' < span class =code-string> area-chart',
data:Graph(),
xkey:' < span class =code-string> year',
ykeys:[' value' ],
async : true ,
标签:[< span class =code-string>' value'],
lineColors:[' #428BCA'],
lineWidth:' 2px',
hideHover: true
});
});
函数Graph(){
var data = < span class =code-string>;
.ajax({
type:' GET',
url:' @ Url.Action(GraphData,Home)',
dataType:' json ',
async : true ,
contentType: application / json; charset = utf-8,
data:{},
成功:函数(结果){
data = result;
},
错误:函数(xhr,状态,错误){
警报(误差);
}
});
返回数据;
}
< / script >
由于对象的详细信息将丢失,因此无法将所需输出转回数据。
序列化必须下降到对象图的最低级别(属性为字符串,日期或数字),然后重新创建该对象图。
output required
result = [Object, Object, Object, Object, Object, Object, Object]
output coming
result = "[{"label":"11-03-2016","value":"159","year":"2000"},{"label":"12-03-2016","value":"3207","year":"2001"},{"label":"13-03-2016","value":"4610","year":"2002"},{"label":"14-03-2016","value":"3418","year":"2003"},{"label":"15-03-2016","value":"551","year":"2004"}]"
What I have tried:
public JsonResult GraphData()
{
GraphModel oGraphModel = null;
List<graphmodel> lstGraphModel = new List<graphmodel>();
int index = 0;
Random random = new Random();
while (index < 5)
{
oGraphModel = new GraphModel();
oGraphModel.label = DateTime.Now.AddDays(index).ToString("dd-MM-yyyy");
oGraphModel.value = random.Next(index, 5000).ToString();
oGraphModel.year = (index + 2000).ToString();
lstGraphModel.Add(oGraphModel);
index++;
}
var jsonSerializer = new JavaScriptSerializer();
string data = jsonSerializer.Serialize(lstGraphModel);
return Json(data, JsonRequestBehavior.AllowGet);
}
<script type="text/javascript">
$(document).ready(function() {
new Morris.Line({
element: 'line-chart',
data: Graph(),
xkey: 'year',
ykeys: ['value'],
async: true,
labels: ['value'],
lineColors: ['#D9534F'],
lineWidth: '2px',
hideHover: true
});
new Morris.Area({
element: 'area-chart',
data: Graph(),
xkey: 'year',
ykeys: ['value'],
async: true,
labels: ['value'],
lineColors: ['#428BCA'],
lineWidth: '2px',
hideHover: true
});
});
function Graph() {
var data = "";
$.ajax({
type: 'GET',
url: '@Url.Action("GraphData", "Home")',
dataType: 'json',
async: true,
contentType: "application/json; charset=utf-8",
data: {},
success: function (result) {
data = result;
},
error: function (xhr, status, error) {
alert(error);
}
});
return data;
}
</script>
解决方案
(document).ready(function() { new Morris.Line({ element: 'line-chart', data: Graph(), xkey: 'year', ykeys: ['value'], async: true, labels: ['value'], lineColors: ['#D9534F'], lineWidth: '2px', hideHover: true }); new Morris.Area({ element: 'area-chart', data: Graph(), xkey: 'year', ykeys: ['value'], async: true, labels: ['value'], lineColors: ['#428BCA'], lineWidth: '2px', hideHover: true }); }); function Graph() { var data = "";
.ajax({ type: 'GET', url: '@Url.Action("GraphData", "Home")', dataType: 'json', async: true, contentType: "application/json; charset=utf-8", data: {}, success: function (result) { data = result; }, error: function (xhr, status, error) { alert(error); } }); return data; } </script>
Your "output desired" cannot be turned back into data as the details of the object will go missing.
Serialisation has to go down to the lowest level of the object graph (the properties as string, date or number) in order to then recreate that object graph.
这篇关于我怎么能得到这个输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文