1

I have a graph in my view which looks like this:

 var hourlyGraph = Morris.Bar({
 element: 'graph_bar',
 data: [
 @foreach (var item in ViewBag.HourlyGraph)
 {
 @:{device: '@item.Hour.ToString("D2"):00', geekbench:@item.Sales },
 }
 ],
 xkey: 'device',
 ykeys: ['geekbench'],
 labels: ['Sold'],
 barRatio: 0.4,
 barColors: ['#0A4D70', '#34495E', '#ACADAC', '#3498DB'],
 xLabelAngle: 35,
 hideHover: 'auto',
 resize: true
 });

This is a morris chart. Note how the data is set up here:

[
@foreach (var item in ViewBag.HourlyGraph)
{
@:{device: '@item.Hour.ToString("D2"):00', geekbench:@item.Sales },
}
]

And now I need to fill the chart with new data. In my Action I have created a list which contains 2 properties:

public int Hour {get;set;}
public int Sales {get;set;}

And they are stored into a list typed of:

var HourlyGraph = new List<HourlyGraph>();

Now I'd like to convert this list into a JSON format which would look something like this:

[
{device: '0', geekbench:5 },
{device: '1', geekbench:13 },
{device: '2', geekbench:25 },
{device: '3', geekbench:14 },
{device: '4', geekbench:16 },
]

Where value for device would be = hour, and geekbench = sales ...

How could I do this in C#?

asked Mar 14, 2017 at 14:08
8
  • Something like this would work ? new JavaScriptSerializer().Deserialize<HourlyGraph>(yourList); Commented Mar 14, 2017 at 14:11
  • Did you have a look at the Newtonsoft.JSON library? You will find it on nuget Commented Mar 14, 2017 at 14:11
  • @TomDoodler yes but what confuses me here is how do I set the first string in json to be named like "device" and "geekbench" ? Commented Mar 14, 2017 at 14:12
  • @joab what about "device" and "geekbench" values, how do I place them in JSON response ? Commented Mar 14, 2017 at 14:13
  • Somethin like this would work ? [JsonProperty(PropertyName = "device")] public int Hour {get;set;} Commented Mar 14, 2017 at 14:15

3 Answers 3

2

With Json.Net and Linq it's easy:

string myJson = 
 JsonConvert.SerializeObject(mylist.Select(item=>
 new {device=item.Hour, geekbench=item.Sales}));

You project an anonymous type with the fields and names that you'd like, and let Newtonsoft.Json do the rest.

answered Mar 14, 2017 at 14:19

9 Comments

Ofir, this is NewtonSoft.JSON library , no ? It says it doesn't contains method "Serialize" ... ?
it's SerializeObject sorry
Ofir, another quick question, says i.Sales doens't exists ... ? Should it be item.Sales or ?
ohhh me and my typos... sure it's item
haha happens , thx a lot =D .. Btw. return type of this action should be string or JsonResponse? Or just a simple string ?
|
1

since you are using mvc why not use return Json() it will convert the object to json string you can use it like

 public ActionResult Myaction()
 { 
 var HourlyGraph = new List<HourlyGraph>();
 return Json(HourlyGraph.Select(x => new {Hour=x.Hour,Sales=x.Sales }));
 }
answered Mar 14, 2017 at 14:25

1 Comment

Usman this looks too good to be true haha... Hold on I'll try =D
0

You can achieve the desired output by using LINQ Anonymous Type

As per example following is class

public class HourlyGraph
{
 public int Hour { get; set; }
 public int Sales { get; set; }
}

Import Namespace System.Web.Script.Serialization which is a Microsoft's Inbuilt Class for dealing with JSON. You will need to refer additional assembly named System.Web.Extensions using 'Add References'.

using System.Web.Script.Serialization;

Declare List and Convert into customized JSON Format

 var listHourlyGraph = new List<HourlyGraph>();
 //Adding some Sample Values
 listHourlyGraph.Add(new HourlyGraph() { Hour = 0, Sales = 5 });
 listHourlyGraph.Add(new HourlyGraph() { Hour = 1, Sales = 10 });
 //Declaring JavaScriptSerialzer Object
 var serialzer = new JavaScriptSerializer();
 //Using Serialize Method which returns back a JSON String
 //We are using LINQ SELECT Method to create a new anonymous return type which contains our custom return types
 string s = serialzer.Serialize(listHourlyGraph.Select(x => new { device = x.Hour, geekbench = x.Sales } ));

You will get the following output in 's' variable

[{"device":0,"geekbench":5},{"device":1,"geekbench":10}]

Note: If you want to get performance optimizations then you are better using Newtonsoft JSON Library instead of Microsoft's Default JSON Library.

answered Mar 14, 2017 at 15:52

1 Comment

Also to add if you want the JSON values as string rather than integer then use C#'s ToString() method in anoumous type. e.g string s = serialzer.Serialize(listHourlyGraph.Select(x => new { device = x.Hour.ToString(), geekbench = x.Sales.ToString() } ));

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.