2012-04-22 82 views
4

我一直在使用GSON庫來解析所有json字符串並獲取JSON對象。 但現在我需要分析是這樣的:如何使用GSON解析Android中的動態「鍵」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 2012-04-22 12:06:22

回答

9

您希望您的模型類看起來如何?

statustotal可能是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結構控制。

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

如果你想在不改變JSON格式,後者模型類,你必須寫:有了這個,你可以只將其與所有喜歡的密鑰訪問,keys()values()好處映射到Map<String, String>一個TypeAdapter(或JsonDeserializer,如果你只是感興趣的解析JSON,而不是從你的模型類生成它)。

這裏有一個JsonDeserializer帽子將您的原始info JSON屬性映射到一個普通的Map<String, String>

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

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

注意不管此註冊了任何Map<String, String>自定義解串器中遇到什麼課了。如果你不想要這個,你還需要創建一個自定義的TypeAdapterFactory,然後在返回之前檢查聲明類以及解串器的實例。

+0

這是工作!謝謝! – gulin 2012-04-22 12:46:15

+1

不客氣:)我只是添加了一個'JsonDeserializer',允許您在不更改JSON格式的情況下使用更好的'Map '。 – 2012-04-22 12:48:04

1

這裏有一個解決方案,它不需要製作JsonDeserializer。

所有你可以創建一個JsonElement在地圖Map.Entry<String, JsonElement>和使用for循環遍歷條目

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

更多here