2011-12-29 23 views
1

主にJSONから逆シリアル化するGSONに問題があります。gsonを入れ子にしたJsonを構文解析する

私は、次のJSONを持っている:

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

私はgsonとそのJSONを表現するクラスを見つける必要があり、私の問題は、(ネストされた「n」は相関機能整数で表される)「ID」

です
+0

もっと具体的になりますか?辞書のIDもわからない。達成するために正確に何か好きですか?あなたの問題は何ですか? –

+0

gsonでそのjsonを表現するクラスを見つけることができず、 "id"はすべての辞書のキーです –

答えて

0

私はあなたが整数であるキーを保存する方法を望んでいると仮定しています。
Json Stringを使用する方法が必要です。彼らは整数であるので、そこにキー

1,2,3....n 

の「n」は数にすることができ、各キーは、それが値になります。

これを行う1つの方法は、HashMapを使用することです。

キーは整数としてマップに格納されます。
無制限の数のキーを使用できます。
簡単にキー値のペアを反復処理します。
異なるオブジェクトを簡単に追加できます。

/*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

ありがとう、たくさんの男!それは非常に便利でした! –

0

私はあなたの最善の策は、動的キーを可能にするためにマップを使用することだと考えています。次のようなものがあります。

あなたのJSONは再帰的な構造を持っているようですが、あなたの例からはっきりと分かりません。あなたの名前と管理者のキーが異なる場合、最終的にはnamesnとadminsnという名前が付けられている場合は、マップ内のキーとしてもそれらを表す必要があります。私はGsonがその再帰的構造を許すことができるのかどうかは不明ですが、あなたが求めている動的キーの問題をマップが解決すると信じています。

もちろん、データがマップに入ったら、どのデータ(1,2、.. n)が使用可能であるかを理解するのはあなたのコード次第です。

+0

偽装複合語^^ – MahdeTo

関連する問題