# GSON wenn das Json verschiedene Klassen enthält



## Fohnbit (3. Feb 2016)

Hallo,

wie könnt ich ein Json mit GSON serialisieren, wenn ich das Format des Json nicht vorab weiß?

Ich erhalte eine Event Nachricht, wo verschiedene Zustände eines Systems dargestellt werden:
Array "evts" enthält 8 Nachrichten.
Beispiel:
evts[0] ist unterschiedlich im Aufbau als evts[4]
Identifizieren kann man es durch das "@self" Feld.
Wenn ich also "@self" zuerst lesen könnte, kann ich die nötige Klasse bestimmen, mit welcher serialisiert werden soll.

Aber wie?



```
{
  "evts": [
    {
      "id": "FFA34CA8F1EC43F190020BED3126D903.0",
      "time": "2016-02-02T19:25:42.245+01:00",
      "type": "CHANGED",
      "props": [
        "bypassed"
      ],
      "evt": {
        "@type": [
          "IN.dsbl.1",
          "IN.byp.1",
          "IN.walkT.1",
          "IN.dev.1",
          "IN.point.1"
        ],
        "@self": "/1.1.Point.1003.6",
        "opState": "OK",
        "enabled": true,
        "bypassed": true,
        "walktest": "NONE",
        "incs": [
         
        ],
        "active": true
      }
    },
    {
      "id": "C0CDA2D92CCB457DA281EE7F6A82FEF8.1",
      "time": "2016-02-02T19:25:42.271+01:00",
      "type": "CHANGED",
      "props": [
        "bypassed"
      ],
      "evt": {
        "@type": [
          "IN.dsbl.1",
          "IN.byp.1",
          "IN.walkT.1",
          "IN.dev.1",
          "IN.point.1"
        ],
        "@self": "/1.1.Point.1004.6",
        "opState": "OK",
        "enabled": true,
        "bypassed": true,
        "walktest": "NONE",
        "incs": [
         
        ],
        "active": true
      }
    },
    {
      "id": "D4974591791F4397B52AECE28C210CEC.2",
      "time": "2016-02-02T19:25:42.293+01:00",
      "type": "CHANGED",
      "props": [
        "readyToArm",
        "numberOfBypassedDevices"
      ],
      "evt": {
        "@type": [
          "IN.area.1"
        ],
        "@self": "/1.1.Area.2.3",
        "armed": false,
        "transitionalState": "",
        "oiiArmable": true,
        "readyToArm": true,
        "readyToDisarm": false,
        "numberOfBypassedDevices": 1,
        "walktest": "",
        "motionDetectorTestActive": false,
        "chimeModeActive": false,
        "incs": [
         
        ]
      }
    },
    {
      "id": "20243E70939C4C77AD06AFE73AE0FD42.3",
      "time": "2016-02-02T19:25:42.302+01:00",
      "type": "CHANGED",
      "props": [
        "readyToArm",
        "numberOfBypassedDevices"
      ],
      "evt": {
        "@type": [
          "IN.area.1"
        ],
        "@self": "/1.1.Area.2.4",
        "armed": false,
        "transitionalState": "",
        "oiiArmable": true,
        "readyToArm": true,
        "readyToDisarm": false,
        "numberOfBypassedDevices": 1,
        "walktest": "",
        "motionDetectorTestActive": false,
        "chimeModeActive": false,
        "incs": [
         
        ]
      }
    },
    {
      "id": "0F2F176E0A394B5AA0B24D3A826F94F7.4",
      "time": "2016-02-02T19:25:42.371+01:00",
      "type": "CHANGED",
      "props": [
        "transitionalState"
      ],
      "evt": {
        "@type": [
          "IN.area.1"
        ],
        "@self": "/1.1.Area.2.2",
        "armed": false,
        "transitionalState": "INEXITDELAY",
        "oiiArmable": true,
        "readyToArm": true,
        "readyToDisarm": false,
        "numberOfBypassedDevices": 0,
        "walktest": "",
        "motionDetectorTestActive": false,
        "chimeModeActive": false,
        "incs": [
         
        ]
      }
    },
    {
      "id": "C6E218F514984BF1BC7D9F833235E2CD.5",
      "time": "2016-02-02T19:25:42.376+01:00",
      "type": "CHANGED",
      "props": [
        "transitionalState"
      ],
      "evt": {
        "@type": [
          "IN.area.1"
        ],
        "@self": "/1.1.Area.2.3",
        "armed": false,
        "transitionalState": "INEXITDELAY",
        "oiiArmable": true,
        "readyToArm": true,
        "readyToDisarm": false,
        "numberOfBypassedDevices": 1,
        "walktest": "",
        "motionDetectorTestActive": false,
        "chimeModeActive": false,
        "incs": [
         
        ]
      }
    },
    {
      "id": "84B0EC7C90DF4B3083C7F1AC98F3AB19.6",
      "time": "2016-02-02T19:25:42.386+01:00",
      "type": "CHANGED",
      "props": [
        "transitionalState"
      ],
      "evt": {
        "@type": [
          "IN.area.1"
        ],
        "@self": "/1.1.Area.2.4",
        "armed": false,
        "transitionalState": "INEXITDELAY",
        "oiiArmable": true,
        "readyToArm": true,
        "readyToDisarm": false,
        "numberOfBypassedDevices": 1,
        "walktest": "",
        "motionDetectorTestActive": false,
        "chimeModeActive": false,
        "incs": [
         
        ]
      }
    },
    {
      "id": "FDAB07CE95674810BDA43E77E7DA48C2.7",
      "time": "2016-02-02T19:25:42.400+01:00",
      "type": "CHANGED",
      "props": [
        "on"
      ],
      "evt": {
        "@type": [
          "IN.dsbl.1",
          "IN.dev.1",
          "IN.output.1"
        ],
        "@self": "/1.1.Output.12001.10",
        "opState": "OK",
        "enabled": true,
        "incs": [
         
        ],
        "on": true
      }
    }
  ]
}
```


----------



## Fohnbit (3. Feb 2016)

Habs nun doch selber noch hinbekommen:

```
JsonParser jsonParser = new JsonParser();
        JsonArray evts = jsonParser.parse(content.replace("@", ""))
                .getAsJsonObject().get("evts").getAsJsonArray();

        int index = 0;
        for (JsonElement result : evts) {
            JsonObject evt = result.getAsJsonObject().get("evt")
                    .getAsJsonObject();

            // was hat sich geändert?
            Props props = gson.fromJson(result, Props.class);
            System.out.println(props.toString());

            // ID bekommen
            String self = evt.get("self").getAsString();
            System.out.println("self: " + self);

            // Device Meldung
            if (self.startsWith("/1.1.Point.")) {
                for (Point point : points.getList()) {
                    if (point.getSelf().equals(self)) {
                        points.updateList(gson.fromJson(evt, Point.class),
                                index);
                    }
                    index++;
                }
            }
            index = 0;

            // Area Meldung
            if (self.startsWith("/1.1.Area.")) {
                for (Area area : areas.getList()) {
                    if (area.getSelf().equals(self)) {
                        // Array Serialisieren??
                        areas.updateList(gson.fromJson(evt, Area.class), index);
                    }
                    index++;
                }
            }
        }

        for (Point point : points.getList()) {
            System.out.println(point.toString());
        }

        for (Area area : areas.getList()) {
            System.out.println(area.toString());
        }
```


----------

