我有以下JSON对象,其中存储了所有数据。
[{"id":1,"CoinValue":"0.01","Count":82,"CoinWeight":76},
{"id":2,"CoinValue":"0.02","Count":86,"CoinWeight":18},
{"id":3,"CoinValue":"0.05","Count":29,"CoinWeight":42},
{"id":4,"CoinValue":"0.1","Count":35,"CoinWeight":90},
{"id":5,"CoinValue":"0.2","Count":23,"CoinWeight":3},
{"id":30,"CoinValue":"0.5","Count":41,"CoinWeight":36}]
我希望获得每条记录的CoinValue和Count,以便在Google图表中显示。目前,我有一个视图在Coin控制器中调用一个名为Data的方法来接收数据,但是我无法使其工作。
CoinController:
public ActionResult Data()
{
//Read Json object
var fileContents = System.IO.File.ReadAllText(@"H:\EasyAsPiMVC\EasyAsPiMVC\App_Data\MOCK_DATA.json");
//not sure how to properly pass this data to the view
return Json(fileContents);
}
用于处理和显示数据的Javascript
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: 'POST',
dataType: "Json",
contentType: "application/json",
url: '@Url.Action("Data", "Coin")',
success: function (result) {
google.charts.load('current', {
'packages': ['corechart']
});
google.charts.setOnLoadCallback(function () {
drawChart(result);
});
}
});
function drawChart(result) {
var data = new google.visualization.DataTable();
data.addColumn("string", "CoinValue"); //might have to be changed
data.addColumn("number", "Count");
var dataArray = [];
$.each(result, function (i, obj) {
dataArray.push([obj.CoinValue, obj.Count]);
});
data.addRows(dataArray);
var piechart_options = {
title: 'Coin Tracker Piechart',
width: 400,
height: 300
};
var piechart = new google.visualization.PieChart(document.getElementById('piechart_div'));
piechart.draw(data, piechart_options);
var barchart_options = {
title: 'Coin Tracker barchart',
width: 400,
height: 300,
legend: 'none'
};
var barchart = new google.visualization.BarChart(document.getElementById('barchart_div'));
barchart.draw(data, barchart_options);
}
});
</script>
我是一个编程菜鸟,现在已经有一天没想到这个了。我做了大量的研究,但我似乎无法做到这一点。
答案 0 :(得分:1)
composer.json
方法为您提供ReadAllText
,即文件的内容。您需要将其反序列化为string
个对象的列表。您的客户端代码需要一组对象,每个对象都具有Coin
和CoinValue
属性。所以你需要返回那个json数组。
以下是使用JSON.NET
的方法Count