How to parse nested objects in JSON response
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
apex json map deserialize nested
add a comment |
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
apex json map deserialize nested
add a comment |
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
apex json map deserialize nested
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
apex json map deserialize nested
apex json map deserialize nested
edited 1 hour ago
Jay
asked 1 hour ago
JayJay
305210
305210
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I think you can address this effectively with a mix of collections and custom classes, like this:
public class countryLocale {
public String preferredLanguage;
public String preferredLocale;
public Map<String, Locale> languages;
}
public class Locale {
public String langCountry;
public String defaultLang;
}
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
countryLocale l = (countryLocale)JSON.deserialize(
jsonstr,
countryLocale.class
);
David thanks so much for the fast answer :) I've driven myself almost insane trying to figure this out today. Greatly appreciated
– Jay
42 mins ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f247838%2fhow-to-parse-nested-objects-in-json-response%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you can address this effectively with a mix of collections and custom classes, like this:
public class countryLocale {
public String preferredLanguage;
public String preferredLocale;
public Map<String, Locale> languages;
}
public class Locale {
public String langCountry;
public String defaultLang;
}
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
countryLocale l = (countryLocale)JSON.deserialize(
jsonstr,
countryLocale.class
);
David thanks so much for the fast answer :) I've driven myself almost insane trying to figure this out today. Greatly appreciated
– Jay
42 mins ago
add a comment |
I think you can address this effectively with a mix of collections and custom classes, like this:
public class countryLocale {
public String preferredLanguage;
public String preferredLocale;
public Map<String, Locale> languages;
}
public class Locale {
public String langCountry;
public String defaultLang;
}
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
countryLocale l = (countryLocale)JSON.deserialize(
jsonstr,
countryLocale.class
);
David thanks so much for the fast answer :) I've driven myself almost insane trying to figure this out today. Greatly appreciated
– Jay
42 mins ago
add a comment |
I think you can address this effectively with a mix of collections and custom classes, like this:
public class countryLocale {
public String preferredLanguage;
public String preferredLocale;
public Map<String, Locale> languages;
}
public class Locale {
public String langCountry;
public String defaultLang;
}
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
countryLocale l = (countryLocale)JSON.deserialize(
jsonstr,
countryLocale.class
);
I think you can address this effectively with a mix of collections and custom classes, like this:
public class countryLocale {
public String preferredLanguage;
public String preferredLocale;
public Map<String, Locale> languages;
}
public class Locale {
public String langCountry;
public String defaultLang;
}
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
countryLocale l = (countryLocale)JSON.deserialize(
jsonstr,
countryLocale.class
);
answered 51 mins ago
David ReedDavid Reed
32.8k72052
32.8k72052
David thanks so much for the fast answer :) I've driven myself almost insane trying to figure this out today. Greatly appreciated
– Jay
42 mins ago
add a comment |
David thanks so much for the fast answer :) I've driven myself almost insane trying to figure this out today. Greatly appreciated
– Jay
42 mins ago
David thanks so much for the fast answer :) I've driven myself almost insane trying to figure this out today. Greatly appreciated
– Jay
42 mins ago
David thanks so much for the fast answer :) I've driven myself almost insane trying to figure this out today. Greatly appreciated
– Jay
42 mins ago
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f247838%2fhow-to-parse-nested-objects-in-json-response%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown