JochemQuery
JochemQuery

Reputation: 1567

Converting a Dictionary to JSON with JSON.NET

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 enter image description here

The result in chrome enter image description here

I've tried a lot of other Stackoverflow answers. I don't know what to try anymore.

Thanks so far!

enter image description here

Upvotes: 1

Views: 8285

Answers (3)

Coder1409
Coder1409

Reputation: 498

the answer should be something like this using jobject (newtonsoft.json.linq)

JObject o = new JObject
        (
            new JProperty("DataProvider",
                new JArray(
                    from m in List
                    select new JObject(
                        new JProperty("title",m.Key),
                        new JProperty("profit",m.Value))))
        );

and that is it you can display by using o.tostring()

the output is something like

{
  "DataProvider": [
    {
      "title": "movie1",
      "profit": 2121.0
    },
    {
      "title": "movie2",
      "profit": 47877.0
    }
  ]
}

Upvotes: 0

Sam
Sam

Reputation: 5014

First, you should remove the TypeNameHandling = TypeNameHandling.All setting. That's the reason your JSON includes the $type property.

Second, you should use @Html.Raw(Model.ProfitListJson) to render your JSON string without &quot.

Something like this in your View:

var jsonObj = @Html.Raw(Model.ProfitListJson);
var chart = AmCharts.makeChart("chart_6", {
    //...
    "dataProvider": jsonObj,
    //...
});

Something like this in your controller:

string json = JsonConvert.SerializeObject(plist, 
  Formatting.Indented, 
  new JsonSerializerSettings
{
    TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple // I don't think this is needed, but leaving it in in case you had a reason for it
});

Upvotes: 1

Praveen Paulose
Praveen Paulose

Reputation: 5771

In order to get a JSON with movie and profit attributes, you will need to create a DTO (Data Transfer Object) E.g.

public class MovieProfit
{
    public string Title { get; set; }
    public double Profit { get; set; }
}

and then convert your dictionary to a list of this DTO using Linq

List<MovieProfit> plist = profitList.Select(keyValuePair => new MovieProfit() { Title = keyValuePair.Key.Title, Profit = keyValuePair.Value }).ToList();

You will now get the desired JSON on serialization.

With regards to the quotes, it is because you are serializing your objects to a JSON string and passing the string back in the ViewModel. If you pass the object or list of objects back, you will not face this problem.

Upvotes: 2

Related Questions