JSON serialization of enum as string in MVC ActionResult

Rahul Kiwitech
Rahul K...
292 Points
26 Posts

I am trying to return json result from mvc action. But when serialize enum property it return the numeric value of the enum. I want to string value of enum during json result. Following is the code sniped:

[HttpGet]
public ActionResult EventOrderDetail(string orderId, long eventId)
{
EventOrderDetailViewModel order = _service.GetOrderDetails(orderId, eventId);
return Json(order, JsonRequestBehavior.AllowGet);
}

 Return Class have definition as

public class EventOrderDetailViewModel
{
public long OId { get; set; }
public string OrderId { get; set; }
public string BuyerName { get; set; }
public string TicketName { get; set; }
public string BuyerEmail { get; set; }
public string CustomerEmail { get; set; }
public long Quantity { get; set; }
public decimal Price { get; set; }
public decimal PricePaid { get; set; }
public decimal PriceNet { get; set; }
public decimal Fee { get; set; }
public decimal MerchantFee { get; set; }
public DateTime Date { get; set; }
public string DateStr { get; set; }
public PaymentStates PaymentState { get; set; }
public string Address { get; set; }
public string PromoCode { get; set; }
public string MailTickets { get; set; }
private List<Event_VariableDesc> _variableChages = new List<Event_VariableDesc>();
public List<Event_VariableDesc> VariableChages
{
get { return _variableChages; }
set { _variableChages = value; }
}
public string VariableIds { get; set; }
public string PhoneNumber { get; set; }
}
 
public enum PaymentStates { Total, Completed, Pending }

Output:

{ "PaymentStates" : 1,...}

I want Output:

{ "PaymentStates" : "Total",...}

How we can solved this?

Thanks in advance

Views: 12664
Total Answered: 3
Total Marked As Answer: 2
Posted On: 12-Dec-2016 03:47

Share:   fb twitter linkedin
Answers
Smith
Smith
2890 Points
78 Posts
         

No there is no special attribute you can use. JavaScriptSerializer serializes enums to their numeric values and not their string representation. You would need to use custom serialization to serialize the enum as its name instead of numeric value.

Posted On: 12-Dec-2016 04:10
Rahul Maurya
Rahul M...
4918 Points
28 Posts
         

You can use Json.NET serializer i.e. by using Newtonsoft's Json. It has a property

[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]

You can use it as attribute on the enum properties. Or you can use as (In your case):

var setting = new Newtonsoft.Json.JsonSerializerSettings();
setting.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());

var serializedObject = Newtonsoft.Json.JsonConvert.SerializeObject(order, setting);
return Content(serializedObject, @"application/json");

 

 

Posted On: 12-Dec-2016 04:28
Brian
Brian
2376 Points
13 Posts
         

I found something like

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

[JsonConverter(typeof(StringEnumConverter))]
public Gender Gender { get; set; }

Use StringEnumConverter  converter to serialize the enum .

Posted On: 08-May-2018 20:07
 Log In to Chat