Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Squishyaura · Sep 21, 2016 at 05:29 PM · jsonconvertenum

JsonData to Enum

Hi Unity commmunity, I have an issue.

I'm using LitJson to make a database of items: alt text

I have a class to create a new item, which has the following parameters: alt text

My issue is that when I try to choose the rarity of the item, which is an enum that cosists of {common, uncommon, rare, legendary, ...}, I get the following problem: https://gyazo.com/921fc188c1f9c83483617cf160d86e9d

How do I convert JsonData to an enum?

7eddcec13596ffe5d07be64192bf3aed.png (13.6 kB)
801bbc33371466d624cdd7e731fcc078.png (32.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Bunny83 · Sep 21, 2016 at 05:44 PM

Well, you need to parse the string you get from your Json data into the appropriate enum value by using System.Enum.Parse:

 (Rarity)System.Enum.Parse(typeof(Rarity), itemData[i]["rarity"])

instead of

 itemData[i]["rarity"]

edit
unfortunately you can't add extension methods to "types" only to "instances". You could add this class to your project:

 public static class EnumExtension
 {
     public static T Parse<T>(this System.Enum aEnum, string aText)
     {
         return (T)System.Enum.Parse(typeof(T), aText);
     }
 }

It will add the Parse method to each enum value. So you could simply do:

 Rarity.common.Parse(itemData[i]["rarity"])

It doesn't matter which enum member you use. Of course instead of an extension method you could simply place the method in a helper class:

 public static class Helper
 {
     public static T ParseEnum<T>(string aText)
     {
         return (T)System.Enum.Parse(typeof(T), aText);
     }
 }

This can be used like this:

 Helper.ParseEnum<Rarity>(itemData[i]["rarity"])
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Squishyaura · Sep 21, 2016 at 07:25 PM 1
Share

Thank you my good sir, have a wonderful day!

avatar image
0

Answer by elenzil · Sep 23, 2016 at 12:06 AM

Bunny's answer is very good. here's the variant i use:

   public static bool ParseStringToEnum<T>(string s, out T enumValue) where T: struct, IComparable, IFormattable, IConvertible {
     foreach (T enumCandidate in System.Enum.GetValues(typeof(T))) {
       if (s == enumCandidate.ToString()) {
         enumValue = enumCandidate;
         return true;
       }
     }
     
     enumValue = default(T);
     
     return false;
   }
 

less performant, but won't throw exception. hm, maybe one could just put a try/catch around System.Enum.Parse().

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

(SOLVED) Serializing different JSON objects, contained in an array, in another JSON object. (C#) 2 Answers

How to set KeyCode with Number for load? C# 1 Answer

Get JSON array object string value 2 Answers

Serialization of an Enum Array 3 Answers

JsonConvert.DeserializeObject Not responding when building IOS project. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges