I've a problem converting a dictonairy to JSON.NET. I'm sure i'm missing some points. Also my experience in working with JSON is small and I mostly did it from php not from c#.
It adds &qout i'm missing
//GENERAL NOTE: IT's a school project (so not much focus on security)
//C#
public ActionResult GetChartData(string startDate, string endDate)
{
Dictionary<Movie, double> profitList = //Gets data from repository
//in the json list i want the movie names not the objects so I make another dictonairy to convert to json
Dictionary<string, double> plist = profitList.ToDictionary(keyValuePair => keyValuePair.Key.Title, keyValuePair => keyValuePair.Value);
//Code from other stackoverflow post
//http://stackoverflow.com/questions/3739094/serializing-deserializing-dictionary-of-objects-with-json-net
string json = JsonConvert.SerializeObject(plist, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
});
//ViewModel i Use
FinancialViewModel viewModel = new FinancialViewModel
{
ProfitList = profitList,
ProfitListJson = json,
Start = start,
End = end
};
return PartialView("_FinancialPartialView", viewModel);
}
//JS
<script>
var chart = AmCharts.makeChart("chart_6", {
"type": "pie",
"theme": "light",
"fontFamily": "Open Sans",
"color": "#888",
"dataProvider": @Model.ProfitListJson,
"valueField": "movie", //the name from the movie
"titleField": "profit", //the profit from the movie
"exportConfig": {
menuItems: [
{
icon: Metronic.getGlobalPluginsPath() + "amcharts/amcharts/images/export.png",
format: "png"
}
]
}
});
</script>
This is the result I want to get
"dataProvider": [{
"movie": "Title of movie 1",
"profit": Profit of movie 1
}, {
"movie": Title 2c",
"profit": Profit 2
}],
"valueField": "movie",
"titleField": "profit",
The current result I get in the controller while debugging
The result in chrome
I've tried a lot of other Stackoverflow answers. I don't know what to try anymore.
Thanks so far!