Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7007891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:39:56+00:00 2026-05-27T21:39:56+00:00

I’m having some trouble with GSON, mainly deserializing from JSON. I have the following

  • 0

I’m having some trouble with GSON, mainly deserializing from JSON.

I have the following JSON:

{
    "1" : { 
        "name" : "NAME",
        "admins" : {
            "1": {
                "name2" : "NAME2",
                "admins2" : {
                    "1": { ... },
                    "2": { ... },
                    ......
                    "n": { ... }
                }
            },
            "2": { ... },
            "3": { ... },
            ......
            "n": { ... }
        } 
    },
    "2" : { ... },
    "3" : { ... },
    ......
    "n" : { ... }
 }

I need to find the Class to represent that json with gson, my problem is the “id” ( represented by the “n” correlatives integers nested)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:39:56+00:00Added an answer on May 27, 2026 at 9:39 pm

    I am assuming you want a way to store keys which are Integers.
    You want a way to use the Json String . Since they are integers, there can be “n” number of keys

    1,2,3....n
    

    and each key will have it values.

    One way to do this is to use a HashMap.

    The keys are stored in the Map as Integers.
    Unlimited number of keys can be used.
    Easily iterate through the key value pair.
    Easily extendisible to add differernt objects.

    /*Here we do the following steps
    *  1. create the data
    *  2. convert data to Json String using GSON
    *  3. JSon String is used to populate the data bean using GSON.
    *
    *  Integers will be used as the key.
    */ 
    public class CreateAccessGSON()
    {
        public static void main(String[] args)
        {
    
             Gson gson = new Gson(); //instantiate gson here.
    
             //Creating the Data Object.
             HashMap<Integer,AdminBean> tmpAdminMap = new HashMap<Integer,AdminBean>();
    
             AdminBean adminOne1 = new AdminBean();
             adminOne1.setName("Joe");
    
             tmpAdminMap.put(1,adminOne1);   // key is an Integer 1
    
             AdminBean adminOne2 = new AdminBean();
             adminOne2.setName("Blow");
    
             tmpAdminMap.put(2,adminOne2);   // key is an Integer 2
    
             //Set the value of the Map.
             DataObjectBean dataObjectBean = new DataObjectBean();
             dataObjectBean.setAdminMap(tmpAdminMap);
    
             String jsonString = gson.toJson(dataObjectBean);
    
             System.out.println(jsonString ); // print the Json String.
              //Output will be as follows
             /*
               {
                 "adminMap" : 
                        { 
                            "1" : {"name":"Joe"} ,
                            "2" : {"name":"Blow"}
                        }
               }
             */
    
    
    
    
             // Code to Convert Json String to the Associated object.
              DataObjectBean accessDataObjectBean = gson.fromJson(jsonString ,DataObjectBean);
              HashMap<Integer,AdminBean> retrieveAdminMap = accessDataObjectBean.getAdminMap();
              System.out.println(retrieveAdminMap.get(1).getName()); // Joe
              System.out.println(retrieveAdminMap.get(2).getName()); // Blow
    
              //get number of keys, we use the hashmap size.
              System.out.println("Num of keys : " + retrieveAdminMap.size()); // Num of keys : 2
    
              // You can use the Java Iterator to access each key and their values
              Set<Integer> setKey = retrieveAdminMap.keySet();
              for( Integer keys : setKey )
              {
                    AdminBean eachAdmin = retrieveAdminMap.get(keys);
                    System.out.println(eachAdmin.getName()); 
              }
        }
    } 
    
    
    
    //This class will store the Admin data. You can have more nested classes here.
    // This class can further have more maps.
    public class AdminBean
    {
        private String name = "";
    
        public String getName()
        { 
            return name;
        }
    
        public String setName(String name)
        {
            this.name = name;
        }
    }
    
    // This main Java Bean which will be used to generate the JSON.
    // Since we need as Integer as key, we use the HashMap to store it.
    // HashMaps will allow storing unlimited Integers.
    public class DataObjectBean{
        private HashMap<Integer,AdminBean> adminMap = new HashMap<Integer,AdminBean>();
        public String getAdminMap ()
        { 
            return adminMap ;
        }
    
        public String setAdminMap (String adminMap )
        {
            this.name = adminMap ;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.