I'm creating a class that creates an object from a Json with multiple attributes and one of the attributes has a number that I would like to replace programmatically with the corresponding value associated with said number from an enum class. The Problem is i'd like to do it in the auto property.

Here is my JSON:

[{"reason":100000003}]

Here are my enum classes:

public enum option1
    {
        val1 = 100000001,
        val2 = 100000002,
        val3 = 100000003,
        val4 = 100000004


    }

    public enum option2
    {
        val1 = 100000001,
        val2 = 100000002,
        val4 = 100000003,
        val5 = 100000004

    }

Here is my Object class:

public class Value{
private string reason;
     [JsonProperty("reason")]
     public string Reason
     {
        get
        {
            return reason;
        }
        set
        {
            reason = Indexer(reason,"test2");
        }
     }
class RootObject
    {
        public List<Value> Value { get; set; }
    }
 }

Here is the function to convert string number to corresponding value

//for this example, index == "test2"
public static string Indexer(string input, string index)
        {
            string result = null;
            if (index.Equals("test") || index.Equals("test2"))
            {
                switch (input)
                {
                    case "100000001":
                        result = Value.option1.val1.ToString();
                        break;
                    case "100000002":
                        result = Value.option1.val2.ToString();
                        break;
                    case "100000003":
                        if (index.Equals("test1"))
                            result = Value.option1.val3.ToString();
                        else
                            result = Value.option2.val4.ToString();
                        break;
                    case "100000004":
                        if (index.Equals("test1"))
                            result =Value.option1.val4.ToString();
                        else
                            result = Value.option2.val5.ToString();
                        break;
                        default:
                        result = null;
                        break;
                 }
            }
            return result;
        }

For my result I would like the Reason field for my Object to be "val4" instead of "100000003" but it isn't working.

0 Answers