c# - Converting a Dictionary to JSON with JSON.NET -
i've problem converting dictonairy json.net. i'm sure i'm missing points. experience in working json small , did php not c#.
it adds &qout i'm missing
//general note: it's school project (so not focus on security) //c# public actionresult getchartdata(string startdate, string enddate) { dictionary<movie, double> profitlist = //gets data repository //in json list want movie names not objects make dictonairy convert json dictionary<string, double> plist = profitlist.todictionary(keyvaluepair => keyvaluepair.key.title, keyvaluepair => keyvaluepair.value); //code 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 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 movie "titlefield": "profit", //the profit movie "exportconfig": { menuitems: [ { icon: metronic.getglobalpluginspath() + "amcharts/amcharts/images/export.png", format: "png" } ] } }); </script> this result want 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 in controller while debugging 
the result in chrome 
i've tried lot of other stackoverflow answers. don't know try anymore.
thanks far!

first, should remove typenamehandling = typenamehandling.all setting. that's reason json includes $type property.
second, should use @html.raw(model.profitlistjson) render json string without ".
something in view:
var jsonobj = @html.raw(model.profitlistjson); var chart = amcharts.makechart("chart_6", { //... "dataprovider": jsonobj, //... }); something in controller:
string json = jsonconvert.serializeobject(plist, formatting.indented, new jsonserializersettings { typenameassemblyformat = formatterassemblystyle.simple // don't think needed, leaving in in case had reason });
Comments
Post a Comment