Json mit Gson in Pojo lesen


#1

Hallo liebe Freunde der Json Verwendung.

Ich habe mal wieder eine Frage. Ich rufe von einer API Daten ab und möchte diese in mein Object mappen.

mit folgendem Code hole ich die Daten und möchte mappen, aber ich bekomme statt der ID, “null”

Hat jemand eine Idee was ich falsch mache?

public static void getProduct(String domain, String apiUser, String apiPassword, String productSKU) {
		ResteasyClient client = new ResteasyClientBuilder().build();
		ResteasyWebTarget webTarget = client.target(domain + "/api/articles/" + productSKU + "?useNumberAsId=true");
		webTarget.register(new BasicAuthentication(apiUser, apiPassword));
		Response response = webTarget.request().accept("application/json").get();
		response.bufferEntity(); //Response nicht schließen
		Gson gson = new Gson();
		String respo = response.readEntity(String.class);
		System.out.println(respo);
		Data data = gson.fromJson(respo, Data.class);
		System.out.println(data.getId()); // hier kommt null, statt meiner erwarteten ID

und hier ist der String, welcher von der API übergeben wird

{"data":{"id":167,"mainDetailId":741,"supplierId":12,"taxId":1,"priceGroupId":null,"filterGroupId":null,"configuratorSetId":null,"name":"Testartikelbezeichnung","description":"","descriptionLong":"<p>beschreibuuuuuuuuuuuuuuuuuuung<\/p>","added":"2016-03-19T00:00:00+0100","active":true,"pseudoSales":0,"highlight":false,"keywords":"","metaTitle":"","changed":"2017-09-30T10:12:00+0200","priceGroupActive":false,"lastStock":false,"crossBundleLook":0,"notification":false,"template":"","mode":0,"availableFrom":null,"availableTo":null,"mainDetail":{"id":741,"articleId":167,"unitId":null,"number":"SW10001","supplierNumber":"","kind":1,"additionalText":"","active":true,"inStock":0,"stockMin":0,"weight":null,"width":null,"len":null,"height":null,"ean":"","purchasePrice":"0","position":0,"minPurchase":1,"purchaseSteps":null,"maxPurchase":null,"purchaseUnit":null,"referenceUnit":null,"packUnit":"","shippingFree":false,"releaseDate":null,"shippingTime":"","prices":[{"id":874,"articleId":167,"articleDetailsId":741,"customerGroupKey":"EK","from":1,"to":"1","price":84.033613445378,"pseudoPrice":0,"percent":0,"customerGroup":{"id":1,"key":"EK","name":"Shopkunden","tax":true,"taxInput":true,"mode":false,"discount":0,"minimumOrder":10,"minimumOrderSurcharge":5}},{"id":875,"articleId":167,"articleDetailsId":741,"customerGroupKey":"EK","from":2,"to":"beliebig","price":0,"pseudoPrice":0,"percent":0,"customerGroup":{"id":1,"key":"EK","name":"Shopkunden","tax":true,"taxInput":true,"mode":false,"discount":0,"minimumOrder":10,"minimumOrderSurcharge":5}}],"attribute":{"id":753,"articleId":167,"articleDetailId":741,"attr1":"","attr2":"","attr3":"","attr4":"","attr5":"","attr6":"","attr7":"","attr8":"","attr9":"","attr10":"","attr11":"","attr12":"","attr13":"","attr14":"","attr15":"","attr16":"","attr17":null,"attr18":"","attr19":"","attr20":""},"configuratorOptions":[]},"tax":{"id":1,"tax":"19.00","name":"19%"},"propertyValues":[],"supplier":{"metaTitle":null,"metaDescription":null,"metaKeywords":null,"id":12,"name":"Testhersteller","image":"","link":"","description":null,"changed":"2016-03-19T10:33:54+0100"},"propertyGroup":null,"customerGroups":[],"images":[],"configuratorSet":null,"links":[],"downloads":[],"categories":[{"id":1342,"name":"Collier"}],"similar":null,"related":null,"details":[],"seoCategories":[]},"success":true}

Und meine Data Klasse

public class Data {

    @SerializedName("id")
    private Integer id;
//getter und setter

#2

Ok nun bin ich einen Schritt weiter, ich habe mal ein “Data” Object erstellt und ein paar werte initalisiert, dann sieht das ganze so aus, wenn ich daraus einen Json String mache;

{
  "id": 23,
  "active": false
}

Also scheint das was mit dem Aufbau des Json String zu tun haben, weil dieser mit einem {“data”: beginnt

Puhh das ist gar nicht so einfach wie ich dachte.


#3

Cool, hab es hinbekommen. dank dieser Seite: http://www.jsonschema2pojo.org/

Einfach den Json String eingeben und es spuckt die fertigen Klassen aus, dass ist mal super!


#4

Tolle Sache, danke für die Info.
Das wäre auch etwas für die Fundgrube…