GSON einlesen von verschachtelter Datenstruktur

jimb0p

Erfahrenes Mitglied
Hallo Zusammen,

ich würde gerne folgende Datenstruktur als Liste in meiner Java Anwendung haben:

Code:
[
    {
        "id":"12",
        "date":"2015-03-11 04:26:30",
        "campaign_id":"2224",
        "event_id":"1212",
        "customer_id":"",
        "order_reference":"100007068",
        "voucher_code":"THX",
        "delayed_until_date":"",
        "value":"88.1900",
        "commission":"8.8190",
        "currency":"EUR",
        "status":"confirmed",
        "comment":"",
        "items":{
            "38304349":{
                "id":"38304349",
                "name":"Name",
                "event_id":"1212",
                "value":"92.3900",
                "commission":"9.2390",
                "voucher_code":"",
                "status":"confirmed"
            },
            "38304351":{
                "id":"38304351",
                "name":"Name",
                "event_id":"1212",
                "value":"-4.2000",
                "commission":"-0.4200",
                "voucher_code":"",
                "status":"confirmed"
            }
        }
    },
    {
        "id":"13",
        "date":"2015-03-11 08:58:57",
        "campaign_id":"2225",
        "event_id":"1212",
        "customer_id":"",
        "order_reference":"100007071",
        "voucher_code":"",
        "delayed_until_date":"",
        "value":"83.9900",
        "commission":"8.3990",
        "currency":"EUR",
        "status":"confirmed",
        "comment":"",
        "items":{
            "38306545":{
                "id":"38306545",
                "name":"Name",
                "event_id":"1212",
                "value":"83.9900",
                "commission":"8.3990",
                "voucher_code":"",
                "status":"confirmed"
            }
        }
    }
]

Dies habe ich wie folgt gemacht:
Java:
    private void readJsonFromApi(String urlString) throws Exception{
        Gson gson = new Gson();   
        String json = readUrl(urlString);
          
        webgainsTransactions = gson.fromJson(json, new TypeToken<List<WebgainsElement>>(){}.getType());
    }

Und hier die dazugehörigen Klassen:
Java:
package de.webgains;

import java.util.List;

public class WebgainsElement {
    private Element element;
   
    public Element getElement() {
        return element;
    }

    public void setElement(Element element) {
        this.element = element;
    }
   
    public static class Item{
        private String id;
        private String name;
        private String event_id;
        private String value;
        private String commission;
        private String voucher_code;
        private String status;
       
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getEvent_id() {
            return event_id;
        }
        public void setEvent_id(String event_id) {
            this.event_id = event_id;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
        public String getCommission() {
            return commission;
        }
        public void setCommission(String commission) {
            this.commission = commission;
        }
        public String getVoucher_code() {
            return voucher_code;
        }
        public void setVoucher_code(String voucher_code) {
            this.voucher_code = voucher_code;
        }
        public String getStatus() {
            return status;
        }
        public void setStatus(String status) {
            this.status = status;
        }
    }
   
    public static class Items{
        private Item item;

        public Item getItem() {
            return item;
        }

        public void setItem(Item item) {
            this.item = item;
        }
    }
   
    public static class Element{
        private String id;
        private String date;
        private String campaing_id;
        private String event_id;
        private String customer_id;
        private String order_reference;
        private String voucher_code;
        private String delayed_until_date;
        private String value;
        private String commission;
        private String currency;
        private String status;
        private String comment;
        private List<Items> items;
       
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
       
        public String getDate() {
            return date;
        }
        public void setDate(String date) {
            this.date = date;
        }

        public String getCampaing_id() {
            return campaing_id;
        }

        public void setCampaing_id(String campaing_id) {
            this.campaing_id = campaing_id;
        }

        public String getEvent_id() {
            return event_id;
        }

        public void setEvent_id(String event_id) {
            this.event_id = event_id;
        }

        public String getCustomer_id() {
            return customer_id;
        }

        public void setCustomer_id(String customer_id) {
            this.customer_id = customer_id;
        }

        public String getOrder_reference() {
            return order_reference;
        }

        public void setOrder_reference(String order_reference) {
            this.order_reference = order_reference;
        }

        public String getVoucher_code() {
            return voucher_code;
        }

        public void setVoucher_code(String voucher_code) {
            this.voucher_code = voucher_code;
        }

        public String getDelayed_until_date() {
            return delayed_until_date;
        }

        public void setDelayed_until_date(String delayed_until_date) {
            this.delayed_until_date = delayed_until_date;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getCommission() {
            return commission;
        }

        public void setCommission(String commission) {
            this.commission = commission;
        }

        public String getCurrency() {
            return currency;
        }

        public void setCurrency(String currency) {
            this.currency = currency;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getComment() {
            return comment;
        }

        public void setComment(String comment) {
            this.comment = comment;
        }
        public List<Items> getItems() {
            return items;
        }
        public void setItems(List<Items> items) {
            this.items = items;
        }
    }

}

Für alle Daten funktioniert es, nur nicht für die Items. Ich habe leider keine Ahnung warum oder was ich falsch gemacht habe. Hätte hier jemand ein Tip für mich. Habe das ganze mit GSON gemacht.

Gruß!
 

jimb0p

Erfahrenes Mitglied
Hi, danke für die Antwort. Habe es angepasst leider bekomme ich immer noch eine NullPointerException wenn ich mit
Java:
webgainsTransactions.get(0).getElement().getItems().size();
auf das erste Element zugreifen und mir die Anzahl der items ausgeben lassen möchte. Hier meine Änderung, vielleicht habe ich auch was falsch gemacht:
Code:
package de.webgains;

import java.util.Map;

public class WebgainsElement {
    private Element element;
   
    public Element getElement() {
        return element;
    }

    public void setElement(Element element) {
        this.element = element;
    }
   
    public static class Item{
        private String id;
        private String name;
        private String event_id;
        private String value;
        private String commission;
        private String voucher_code;
        private String status;
       
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getEvent_id() {
            return event_id;
        }
        public void setEvent_id(String event_id) {
            this.event_id = event_id;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
        public String getCommission() {
            return commission;
        }
        public void setCommission(String commission) {
            this.commission = commission;
        }
        public String getVoucher_code() {
            return voucher_code;
        }
        public void setVoucher_code(String voucher_code) {
            this.voucher_code = voucher_code;
        }
        public String getStatus() {
            return status;
        }
        public void setStatus(String status) {
            this.status = status;
        }
    }
   
    public static class Items{
        private Item item;

        public Item getItem() {
            return item;
        }

        public void setItem(Item item) {
            this.item = item;
        }
    }
   
    public static class Element{
        private String id;
        private String date;
        private String campaing_id;
        private String event_id;
        private String customer_id;
        private String order_reference;
        private String voucher_code;
        private String delayed_until_date;
        private String value;
        private String commission;
        private String currency;
        private String status;
        private String comment;
        private Map<String, Items> items;
       
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
       
        public String getDate() {
            return date;
        }
        public void setDate(String date) {
            this.date = date;
        }

        public String getCampaing_id() {
            return campaing_id;
        }

        public void setCampaing_id(String campaing_id) {
            this.campaing_id = campaing_id;
        }

        public String getEvent_id() {
            return event_id;
        }

        public void setEvent_id(String event_id) {
            this.event_id = event_id;
        }

        public String getCustomer_id() {
            return customer_id;
        }

        public void setCustomer_id(String customer_id) {
            this.customer_id = customer_id;
        }

        public String getOrder_reference() {
            return order_reference;
        }

        public void setOrder_reference(String order_reference) {
            this.order_reference = order_reference;
        }

        public String getVoucher_code() {
            return voucher_code;
        }

        public void setVoucher_code(String voucher_code) {
            this.voucher_code = voucher_code;
        }

        public String getDelayed_until_date() {
            return delayed_until_date;
        }

        public void setDelayed_until_date(String delayed_until_date) {
            this.delayed_until_date = delayed_until_date;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getCommission() {
            return commission;
        }

        public void setCommission(String commission) {
            this.commission = commission;
        }

        public String getCurrency() {
            return currency;
        }

        public void setCurrency(String currency) {
            this.currency = currency;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getComment() {
            return comment;
        }

        public void setComment(String comment) {
            this.comment = comment;
        }
        public Map<String, Items> getItems() {
            return items;
        }
        public void setItems(Map<String, Items> items) {
            this.items = items;
        }
    }

}
 

Bratkartoffel

gebratene Kartoffel
Premium-User
Hi,

warum hast du eine extra Klasse "Items", dir reicht doch eigentlich "Item"?

Habe das ganze mal kurz zusammengefasst, wie sieht es mit folgendem Stand aus:
Java:
import java.util.List;
import java.util.Map;

public class Application {
   public static class WebgainTransaction {
     public static class Item {
       private String id;
       private String name;
       private String event_id;
       private String value;
       private String commission;
       private String voucher_code;
       private String status;

       // setter & getter
     }

     private String id;
     private String date;
     private String campaing_id;
     private String event_id;
     private String customer_id;
     private String order_reference;
     private String voucher_code;
     private String delayed_until_date;
     private String value;
     private String commission;
     private String currency;
     private String status;
     private String comment;
     private Map<String, Item> items = new HashMap<String, Item>(); // initialize!

     // setter & getter
   }


   // example usage
   private List<WebgainTransaction> webgainsTransactions;

   private void readJsonFromApi(String urlString) throws Exception {
     Gson gson = new Gson();
     String json = readUrl(urlString);

     webgainsTransactions = gson.fromJson(json,
         new TypeToken<List<WebgainTransaction>>() {
         }.getType());
   }
}

Hat es einen speziellen Grund warum du nur mit Strings statt den "richtigen" Datentypen arbeitest?
// edit: uups, sind ja alles String ;)

Grüsse,
BK
 
Zuletzt bearbeitet:

jimb0p

Erfahrenes Mitglied
Super vielen Dank so klappt es! Ja ich verwende nur String weil manche Variablen die normalerweise int wären können auch leer sein dann erhalte ich eine NumberFormatException. Mit String funktioniert es dann halt. Wie würde hier eine bessere Lösung aussehen?

Gruß!
 

Bratkartoffel

gebratene Kartoffel
Premium-User
Hi,

wenn du nicht die primitiven Datentypen (int, long) verwendest, sondern die Klassen (Integer, Long), dann können diese auch null sein. Kenne mich mit GSON aber nicht aus, keine Ahnung ob der das dann auch richtig mappt. Denke aber schon dass es so geht :)

Grüssse,
BK