您所要做的就是創建自定義映射。由於您的JSON文件是相當複雜的,你可以嘗試自動映射發電機,但如果他們不能以任何理由(動態屬性,多態值,不正確的camelCaseNaming檢測等),你總是可以創建自定義映射:
final class Response {
@SerializedName("categories") final List<Category> categories = null;
@SerializedName("facettes") final List<Facet> facettes = null;
@SerializedName("productlistentries") final List<Map<String, Product>> productListEntries = null;
@SerializedName("last") final boolean isLast = Boolean.valueOf(false);
}
final class Category {
@SerializedName("amount") final int amount = Integer.valueOf(0);
}
final class Facet {
// ???
}
final class Product {
@SerializedName("name") final String name = null;
@SerializedName("modelNumber") final int modelNumber = Integer.valueOf(0);
@SerializedName("brandLogo") final String brandLogo = null;
@SerializedName("detailLink") final String detailLink = null;
@SerializedName("online") final boolean isOnline = Boolean.valueOf(false);
@SerializedName("imageURL") final String imageUrl = null;
@SerializedName("addToBasketUrl") final String addToBasketUrl = null;
@SerializedName("rating") final int rating = Integer.valueOf(0);
@SerializedName("ratingCount") final int ratingCount = Integer.valueOf(0);
@SerializedName("features") final List<Feature> features = null;
@SerializedName("price") final String price = null;
@SerializedName("vatLabel") final String vatLabel = null;
@SerializedName("fees") final List<Fee> fees = null;
@SerializedName("gtm") final Gtm gtm = null;
@SerializedName("productComparison") final ProductComparison productComparison = null;
@SerializedName("productWishlist") final ProductWishlist productWishlist = null;
@SerializedName("clubProduct") final boolean isClubProduct = Boolean.valueOf(false);
@SerializedName("onlineOnlyProduct") final boolean isOnlineOnlyProduct = Boolean.valueOf(false);
}
final class Feature {
@SerializedName("key") final String key = null;
@SerializedName("value") final String value = null;
}
final class Fee {
@SerializedName("value") final String value = null;
@SerializedName("dataLayer") final String dataLayer = null;
}
final class Gtm {
@SerializedName("name") final String name = null;
@SerializedName("id") final String id = null;
@SerializedName("price") final String price = null;
@SerializedName("brand") final String brand = null;
@SerializedName("category") final String category = null;
@SerializedName("dimension9") final String dimension9 = null;
@SerializedName("dimension10") final String dimension10 = null;
}
final class ProductComparison {
@SerializedName("dataLayer") final String dataLayer = null;
@SerializedName("dataUrl") final String dataUrl = null;
@SerializedName("text") final String text = null;
@SerializedName("additionalClasses") final String additionalClasses = null;
}
final class ProductWishlist {
@SerializedName("requestUrl") final String requestUrl = null;
@SerializedName("text") final String text = null;
}
手工編寫映射大約需要15分鐘,所以他們可能會有錯誤或錯別字。請注意,我假設您的回覆是隻讀的,不應該手動創建,以便發送到其他地方,因此所有字段均聲明爲final
。關於原始字段的一個說法是:如果使用0
或false
,那麼Java編譯器可以內聯已知編譯時常量,所以Type.value(...)
是一種讓javac認爲它是不能內聯的運行時值的作弊行爲。 (您可能想要生成getter,但簡單數據庫的恕我直言字段更容易使用,並且添加的噪音更少)。
您只需將Call<Map<String, Object>>
更改爲Call<Response>
即可。在香草爪哇
實施例,不改造:
try (final Reader reader = getPackageResourceReader(Q43535942.class, "response.json")) {
final Response response = gson.fromJson(reader, Response.class);
System.out.println(response.productListEntries.get(1).get("3486143").imageUrl);
}
輸出:
//picscdn.redblue.de/doi/pixelboxx-mss-70874441/mobile_220_310_png/CRUNCH-GTO-4125 -Verst%C3%A4rker-%28Class-D%29
上面的鏈接已損壞。你可以發佈JSON文件嗎? –
我嘗試了兩個不同網絡的鏈接,從中爲它工作。我會將其粘貼一些 – 4ndro1d
剛剛添加了一個pastebin鏈接以及 – 4ndro1d