0

I am using below code to convert my model class to JSON data

[DataContract]
public  class EventList
{
    [DataMember(Name = "success")]
    public int success;
    [DataMember(Name = "result")]
    public List<CalendarEvent> Result;
}
[DataContract]
public class CalendarEvent
{
    [DataMember(Name = "id")] 
    public int Id {get;set; }
    [DataMember(Name = "title")] 
    public string Title { get; set; }
    [DataMember(Name = "url")] 
    public string Url { get; set; }
    [DataMember(Name = "class")]
    public string EventClass { get; set; }//event-warning event-success event-special  event-important  event-inverse
    [DataMember(Name = "start")] 
    public long StartTime { get; set; }
    [DataMember(Name = "end")] 
    public long EndTime { get; set; }
}

  EventList model = new EventList();
//Load data 
    MemoryStream stream1 = new MemoryStream();
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(EventList));
    ser.WriteObject(stream1, model);
    stream1.Position = 0;
    StreamReader sr = new StreamReader(stream1);            
    string json_data= sr.ReadToEnd();

The generated JSON looks like this when i am opening from browser

//"{\"result\":[{\"class\":\"event-warning\",\" 

So how can i get rid of forward slashes . The question is not about serialize to JSON and showing custom properties, but about removing slashes from response

Returning model directly instead of parsed JSON is a way , but in my case i have a property name "class" inside CalendarEvent class which i cant modify , since the plugin [ https://github.com/Serhioromano/bootstrap-calendar ] i am using demanding the structure like this So i have to use JSON Serializationa anyway So how can i get rid of this forward slash and unwanted quotes issue?

dbc
  • 104,963
  • 20
  • 228
  • 340
Sebastian
  • 4,625
  • 17
  • 76
  • 145
  • Rather than returning a string, you need to make asp.net-mvc-3 use `DataContractJsonSerializer`. To do that, return a custom `ActionResult`, as is shown here: https://stackoverflow.com/questions/7260924/mvc3-json-serialization-how-to-control-the-property-names – dbc Apr 17 '15 at 14:47
  • 1
    Also, though I don't recommend it, you can have a property named `class` by [using the `@` prefix](https://msdn.microsoft.com/en-us/library/aa664670%28v=vs.71%29.aspx), i.e. `public string @class { get; set; }`. – dbc Apr 17 '15 at 16:19

0 Answers0