0

I have Json Like value name pair. I need to convert to Generic list.

{ "options": { "32": "S", "4": "SM", "33": "M", "34": "L", "35": "XL", "37": "XXL", "38": "XXXL" } }

My class like

public class Options
{
    public int optionId { get; set; }
    public string OptionName { get; set; }
}

How to convert json to List<Options>

0

1 Answer 1

2

First convert your json to Dictionary<string, string> and then convert it to your model like this:

  public class OptionDictionary
    {
        public Dictionary<string, string> Options { get; set; }
    }
    public class Options
    {
        public int OptionId { get; set; }
        public string OptionName { get; set; }
    }
  
  var list = JsonConvert.DeserializeObject<OptionDictionary>(json).Options.Select(x=>new Options(){OptionId = int.Parse(x.Key), OptionName = x.Value}).ToList();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.