1

I am just getting into JSON and all that and I ran into a snag. I am trying to parse a string abbreviation. I want to parse the string abbreviation into an Enum. Lets say my strings are:

'Apl', 'Orng', 'Bna'

Which for this example mean apple, orange, banana. Is there a way with JSON.NET to parse the abbreviated strings into an enum?

*Id prefer it if my enum can have the full name (Apple, Orange, Banana)

1
  • I'm looking for the same solution. My incoming JSON (which I have no control over) is very terse and compact. I need to deserialize the string values into values of a couple different enums in app code. I've looked at custom converters, but don;t think I have it right...and there's not many good examples. Commented Oct 10, 2011 at 19:40

1 Answer 1

2

I think you're supposed to do this:

[DataContract]
public enum Fruit
{
    [EnumMember(Value = "Apl")]
    Apple,

    [EnumMember(Value = "Orng")]
    Orange,

    [EnumMember(Value = "Bna")]
    Banana,

}
Sign up to request clarification or add additional context in comments.

1 Comment

what happens if a new fruit comes from the server that's not in the enum? E.g. "Pear"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.