Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6756597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:32:36+00:00 2026-05-26T13:32:36+00:00

Possible Duplicate: JSON serialization of c# enum as string I have two classes as

  • 0

Possible Duplicate:
JSON serialization of c# enum as string

I have two classes as follows:

Transaction
    int OrderNumber
    // ... snip ...
    IEnumerable<Item> Items

Item
    string Sku
    // ... snip ...
    ItemCategory Category

ItemCategory is an enum that looks like this:

[DataContract]
public enum ItemCategory
{
    [EnumMember(Value = "Category1")]
    Category1,

    [EnumMember(Value = "Category2")]
    Category2
}

My two classes are decorated with the DataContract and DataMember attributes as appropriate.

I am trying to get a JSON representation of the Transaction item. Within Transaction, I have a public method that looks like this:

public string GetJsonRepresentation()
{
    string jsonRepresentation = string.Empty;

    DataContractJsonSerializer serializer = new DataContractJsonSerializer(this.GetType());
    using (MemoryStream memoryStream = new MemoryStream())
    {
        serializer.WriteObject(memoryStream, this);
        jsonRepresentation = Encoding.Default.GetString(memoryStream.ToArray());   
    }

    return jsonRepresentation;
}

This is returning a string that looks like this:

{
    "OrderNumber":123,
    "Items":[{"SKU": "SKU1","Category": 0}]
}

This is what I want, except for the fact that the “Category” enum value for each Item is being serialized as its integer value, instead of the value I am specifying in the EnumMember attribute. How can I get it so the JSON returned looks like “Category”: “Category1” instead of “Category”: 0?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:32:37+00:00Added an answer on May 26, 2026 at 1:32 pm

    Please take look at JSON serialization of enum as string in stack overflow.
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: String vs StringBuilder Hi, I'm creating a json string. I have some
Possible Duplicate: Javascript Objects: Dynamic variable names? I have a json string passed in
Possible Duplicate: How to parse JSON easily? I have this string: [{text: 'First Option',
Possible Duplicate: jQuery: How to stop AJAX function escaping JSON string used to POST
Possible Duplicate: JSON Array iteration in Android/Java I am fetching JSON string from server
Possible Duplicate: How to parse JSON in iOS App I have the following json:
Possible Duplicate: Can't decode JSON string in php I'm at my wits end here,
Possible Duplicate: A better Java JSON library? Hi, I have a json file like
Possible Duplicate: PHP : Create array for JSON I have an array like this
Possible Duplicate: How to get and parse JSON using objective C? Recently I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.