How to parse nested objects in JSON response












1















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










share|improve this question





























    1















    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










    share|improve this question



























      1












      1








      1








      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










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago







      Jay

















      asked 1 hour ago









      JayJay

      305210




      305210






















          1 Answer
          1






          active

          oldest

          votes


















          3














          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
          );





          share|improve this answer
























          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          3














          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
          );





          share|improve this answer
























          • 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
















          3














          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
          );





          share|improve this answer
























          • 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














          3












          3








          3







          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
          );





          share|improve this answer













          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
          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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



















          • 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Ponta tanko

          Tantalo (mitologio)

          Erzsébet Schaár