-4

my json is as given below. i need to convert it into c# class. Please note all values will be different in actual scenario.

{
  'aa-AA': {
    lanCODE: 'aa-AA',
    genNames: {
      female: ['Wavenet'],
      male: ['Bavenet', 'Bavenet'],
    },
    default: 'Wavenet',
    systemLocale: ['ara', 'aru', 'are', 'aro', 'arh', 'arm', 'arq', 'ark'],
    name: 'xxxx',
  },
  'aa-AA': {
    lanCODE: 'aa-AA',
    genNames: {
      female: ['Wavenet'],
      male: ['Bavenet', 'Bavenet'],
    },
    default: 'Wavenet',
    systemLocale: ['ara', 'aru', 'are', 'aro', 'arh', 'arm', 'arq', 'ark'],
    name: 'xxxx',
  },
  'aa-AA': {
    lanCODE: 'aa-AA',
    genNames: {
      female: ['Wavenet'],
      male: ['Bavenet', 'Bavenet'],
    },
    default: 'Wavenet',
    systemLocale: ['ara', 'aru', 'are', 'aro', 'arh', 'arm', 'arq', 'ark'],
    name: 'xxxx',
  }
 }
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
questp
  • 133
  • 1
  • 3
  • 11
  • 3
    Does the complexity of the JSON cames from the fact that it's ill formated and not valid ? – Self Apr 26 '21 at 14:16
  • Does this answer your question? [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/questions/21611674/how-to-auto-generate-a-c-sharp-class-file-from-a-json-string) – Self Apr 26 '21 at 14:17
  • Have you tried anything yet? – DavidG Apr 26 '21 at 14:20
  • "You need" is different than what you have tried. Please post what you have tried in c# and the challenges it has presented to to so we may help you fix it. – Mark Schultheiss Apr 26 '21 at 14:20
  • Format error are : using single quote `'` instead of double quote, Property name must be double quoted `"`. Trailling comma are illegal ([1,2,3 **,** ]=> illegal) – Self Apr 26 '21 at 14:26
  • step 1: make sure your JSON is valid. step 2: ***write at least some code yourself***. step 3: ask again when you have a _specific_ problem, instead of a generic task that's been asked and answered thousands of times before. – Franz Gleichmann Apr 26 '21 at 14:28
  • And multiple properties named `"aa-AA"`, while it's legal (https://stackoverflow.com/questions/5306741/do-json-keys-need-to-be-unique). It's undefined behavior, where the last properties will eraze the previous value. – Self Apr 26 '21 at 14:29
  • Not that a complex json. What is your problem? – Cleptus Apr 26 '21 at 14:31

1 Answers1

0

The initial property is almost certainly meant to be a dictionary key, so I would go with something like this:

public class Language
{
    [JsonProperty("lanCODE")]
    public string LanguageCode { get; set; }
    
    public string Default { get; set; }

    public List<string> SystemLocale { get; set; }

    public GenNames GenNames { get; set; }
}

public class GenNames
{
    public List<string> Female { get; set; }
    public List<string> Male { get; set; }
}

And deserialise like this:

var languages = JsonConvert.DeserializeObject<Dictionary<string, Language>>(json);
DavidG
  • 113,891
  • 12
  • 217
  • 223