2012-04-22 18 views
4

GSONライブラリを使用してjson文字列をすべて解析し、JSONオブジェクトを取得しています。 しかし、今、私が解析する必要がある。このようなものです:キーフィールドは動的でGSONを使用してアンドロイドの動的 "キー"でJSONを解析する方法

{ 
    "status":1, 
    "info":[ 
     { 
     "\u5a31\u4e50":"\u51b7\u76d8,\u9ad8\u811a\u676f,\u6211\u7684\u7cd6\u679c\u5c4b,\u670d\u52a1\u4e1a\u6d88\u8d39\u52b5" 
     }, 
     { 
     "\u7f8e\u5986":"\u4e2a\u62a4|\u5316\u5986#\u9762\u90e8\u62a4\u7406,\u4e2a\u4eba\u536b\u751f,\u8eab\u4f53\u62a4\u7406,\u9999\u6c34\u9999\u6c1b,\u6c90\u6d74|\u7f8e\u53d1\u7528\u54c1,\u5f69\u5986,\u7cbe\u6cb9SPA,\u773c\u90e8\u62a4\u7406,\u78e8\u7802\u53bb" 
     }, 
     { 
     "\u8863\u670d":"\u670d|\u9970|\u978b|\u5e3d#\u670d\u88c5,\u978b\u9774,\u5185\u8863,\u914d\u9970,\u536b\u8863,\u4f11\u95f2\u88e4,T\u6064,\u88d9\u5b50,\u886c\u886b,\u9488\u7ec7\u886b,\u5a74\u5e7c\u513f\u670d\u9970" 
     } 
    ], 
    "total":3 
} 

ので、私はこれを読むためにモデルクラスを作成する方法を知りません。

+0

?私はキーの内容が動的かもしれないと思った...自分でjsonの文字列を手動で生成していますか? – waqaslam

答えて

9

モデルクラスをどのように見たいですか?

statusおよびtotalはおそらくintとなるので、葉はinfoとなります。

実験として、フィールドObject infoを追加して、GsonがArrayList<LinkedHashMap<String, String>>に設定する方法を見てください。醜いですが、キーでアクセスするのは難しいですが、すべてのデータはそこにあります。その情報を考えると、クラスをモデル化するための最速の方法は次のようになります。

class Something { 
    int status; 
    List<Map<String, String> info; 
    int total; 
} 

あなたはそのJSONを生成する方法を管理している場合は、私はちょうどオブジェクト{a:b,c:d,e:f}にオブジェクト[{a:b},{c:d},{e:f}]の配列からinfoの構造を変更することをお勧め。これで、あなただけのキーによるアクセスなどのすべての利点とMap<String, String>keys()values()にマップできます。

class Something { 
    int status; 
    Map<String, String> info; 
    int total; 
} 

あなたはJSON形式を変更することなく、後者のモデルクラスを使用する場合は、あなたが記述する必要がありますTypeAdapter(またはJSONの解析に興味があり、モデルクラスから生成しない場合はJsonDeserializer

ここでは、オリジナルのinfo JSONプロパティをプレーンMap<String, String>にマップするJsonDeserializerハットを示します。これは、それが発生したものをクラスに関係なく、すべてのMap<String, String>のカスタムデシリアライザを登録すること

GsonBuilder builder = new GsonBuilder(); 
builder.registerTypeAdapter(
    new TypeToken<Map<String, String>>() {}.getType(), 
    new ArrayOfObjectsToMapDeserializer()); 
Gson gson = builder.create(); 

注:

class ArrayOfObjectsToMapDeserializer 
    implements JsonDeserializer<Map<String, String>> { 

    public Map<String, String> deserialize(JsonElement json, Type typeOfT, 
     JsonDeserializationContext context) throws JsonParseException { 
    Map<String, String> result = new HashMap<String, String>(); 

    JsonArray array = json.getAsJsonArray(); 
    for (JsonElement element : array) { 
     JsonObject object = element.getAsJsonObject(); 
     // This does not check if the objects only have one property, so JSON 
     // like [{a:b,c:d}{e:f}] will become a Map like {a:b,c:d,e:f} as well. 
     for (Entry<String, JsonElement> entry : object.entrySet()) { 
     String key = entry.getKey(); 
     String value = entry.getValue().getAsString(); 
     result.put(key, value); 
     } 
    } 
    return result; 
    } 
} 

あなたはこれに似たこのカスタムJsonDeserializerを登録する必要があります。これを望まない場合は、カスタムTypeAdapterFactoryを作成して、デシリアライザを返す前に宣言クラスをチェックする必要があります。

+0

それは仕事です!ありがとう! – gulin

+1

あなたは大歓迎です:) JSON形式を変更せずにもっと良い 'Map 'を使うことを可能にする 'JsonDeserializer'を追加しました。 –

1

ここでは、JsonDeserializerを作成する必要はありません。

すべてのユーザーが作成することができ、地図Map.Entry<String, JsonElement>にJsonElementあると

//parsing string response to json object 
JsonObject jsonObject = (JsonObject) new JsonParser().parse(jsonString); 
    //getting root object 
JsonObject dateWiseContent = jsonObject.get("rootObject").getAsJsonObject(); 

     for (Map.Entry<String, JsonElement> entry : dateWiseContent.entrySet()) { 

        //this gets the dynamic keys 
        String dateKey = entry.getKey(); 

        //you can get any thing now json element,array,object according to json. 

        JsonArray jsonArrayDates = entry.getValue().getAsJsonArray(); 
     } 

が読んエントリを反復するforループを使用して複数のキーが動的であるか来るhere

関連する問題