Im trying to parse the below JSON response:
{
"preferredLanguage":"en",
"preferredLocale":"en_GB",
"languages":{
"en":{
"langCountry":"en-bn",
"defaultLang":"en_GB"
}
}
}
I can do this successfully if I create 3 classes with the third called 'en'. However I would like to avoid this as there may be other languages besides English. Is there a way to specify a key of 'en' to return the data within the 'en' object? I have tried JSON.deserialiseUntyped with Maps of String, Object but I just get the error:
System.TypeException: Invalid conversion from runtime type List<ANY> to Map<String, Object>
Here is the working class structure where I have hardcoded the class name "En":
public class countryLocale {
public String preferredLanguage;
public String preferredLocale;
public Languages languages;
public class En {
public String langCountry;
public String defaultLang;
}
public class Languages {
public En en;
}
Thanks in advance
Best Answer
I think you can address this effectively with a mix of collections and custom classes, like this:
That saves you from having to model each language statically as a property in a
Languages
class. Then, you can deserialize the data by doing