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

  • Home
  • SEARCH
  • 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 9316991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:45:57+00:00 2026-06-19T02:45:57+00:00

I am attempting to recreate from a JSON string, a collection that holds enum

  • 0

I am attempting to recreate from a JSON string, a collection that holds enum constants (the source of the JSON string). I’m having difficulty figuring out how to recreate the enum from a string. The enums hold a variety of methods which I need access to.

The answers I’ve seen this and this one, result in a class which enables you to iterate through the constants rather than recreating the original enum. I want to pass the enums around other existing classes without having to rewrite code to cope with a new class.

Is this even possible? Can you use a JSON string to help recreate an enum in such a way you can use it as an enum?

EDIT: Bizarre, I have a big red banner telling me Stack Overflow requires external Javascript and now I cannot comment on posts…

Thanks Brian for your reply below, but it appears in essence to be the same as the two I linked to here, it does not result in you actually having the enum object, just the constants via a different method.

  • 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-06-19T02:45:58+00:00Added an answer on June 19, 2026 at 2:45 am

    An enum in Java is a class, and the compiler adds a static valueOf(String name) method. So if you have:

    enum MyEnum  { 
          ONE(1.0), TWO(2.0), THREE(3.0); 
    
          private double myDouble; 
    
          MyEnum(double d) {  
              myDouble = d;
          }
    
          public double getDouble() {
              return myDouble;
          }
    }
    

    You can say:

    MyEnum e = MyEnum.valueOf("ONE");
    assert(e.equals(MyEnum.ONE));
    System.out.println(e.getDouble());
    

    The more popular JSON parsers for Java handle enums just fine. Here’s an example with Gson:

    public class App
    {
    
        public static void main(String[] args) 
        {
            // Array containing your enum constants
            String json = "[\"ONE\",\"TWO\",\"THREE\"]";
    
            Type t = new TypeToken<Collection<MyEnum>>(){}.getType();
            Collection<MyEnum> c = new Gson().fromJson(json, t);
    
            for (MyEnum me : c)
            {
                System.out.println(me);
                switch(me)
                {
                    case ONE:
                        System.out.println("This is equal to MyEnum.ONE");
                        break;
                    case TWO:
                        System.out.println("This is equal to MyEnum.TWO");
                        break;
                    case THREE:
                        System.out.println("This is equal to MyEnum.THREE");
                        break;
                }
                System.out.println(me.getDouble());
    
            }
        }    
    }
    

    Output:

    ONE
    This is equal to MyEnum.ONE
    1.0
    TWO
    This is equal to MyEnum.TWO
    2.0
    THREE
    This is equal to MyEnum.THREE
    3.0

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

Sidebar

Related Questions

I am attempting to drop and recreate a database from my CI setup. But
I am attempting to recreate an effect that I found on ( another site
Attempting to parse json from a url requiring login. Including all my code here
Attempting to get an enumerated value by an index from a NSMutableArray property that
Here is the json that I am struggling to recreate. : WorkLoadResult({ AreaReturned:[ {
Attempting to use the data series from this example no longer passes the JSONLint
Attempting to make a NSObject called 'Person' that will hold the login details for
I am attempting to recreate these two content boxes stacked ontop of one another
I'm attempting to recreate a time sheet built on asp and I can't figure
I am attempting to create a SVG Pie Graph that is 277 degrees. I

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.