Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am assuming you want a way to store keys which are Integers.<br> You want a way to use the Json String . Since they are integers, there can be "n" number of keys<br></p> <pre><code>1,2,3....n </code></pre> <p>and each key will have it values.</p> <p><strong>One way to do this is to use a HashMap.</strong></p> <p>The keys are stored in the Map as Integers. <br> Unlimited number of keys can be used.<br> Easily iterate through the key value pair.<br> Easily extendisible to add differernt objects.</p> <pre><code>/*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&lt;Integer,AdminBean&gt; tmpAdminMap = new HashMap&lt;Integer,AdminBean&gt;(); 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&lt;Integer,AdminBean&gt; 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&lt;Integer&gt; 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&lt;Integer,AdminBean&gt; adminMap = new HashMap&lt;Integer,AdminBean&gt;(); public String getAdminMap () { return adminMap ; } public String setAdminMap (String adminMap ) { this.name = adminMap ; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload